[Unsolved]SQLite SELECT Problem (NUllPointerException)

Discussion in 'Plugin Development' started by Chlorek, Aug 6, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Chlorek

    Hello, I am using that to handle my SQLite database. And I got some problem with that:
    Code:
    ResultSet result = db.query("SELECT `player`,`exp` FROM `levels` WHERE `player`='" + name + "'");
    Well I am getting NullPointerException to this line. I do not know why, because database is valid and contains requested data. What's wrong?
     
  2. Offline

    rjVapes

    If you're getting an NPE on that line it has to be from 'db' being null. Toss a check in before it just to verify then start troubleshooting it, run any checkConnection type function to see if it can establish a connection, make sure you open the connection before running the query, etc.
     
  3. Offline

    Chlorek

    Right some time before that I am sending query to insert some value to database and it works.

    Sry for double-post, but please help! I need that to get fixed right today!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  4. Post more surrounding code because there's nothing else that can trigger a NPE on that line than the "db" variable.
    And you should double-check if that is indeed the line where the NPE triggers... but you should also post it, I want to make sure you looked in the right place :} maybe it's even my class' fault xD

    Also, even if "name" variable is null, it won't trigger a NPE because you're not accessing its methods, you're 'printing' its value.
     
  5. Offline

    Chlorek

    Well...
    For connecting to database:
    Code:
    db = new Db(plugin, "plugins" + File.separator + "clRPG" + File.separator + "database.db");
         
            db.query("CREATE TABLE IF NOT EXISTS `levels` (`player` TEXT PRIMARY KEY, `exp` INTEGER)");
            db.query("CREATE TABLE IF NOT EXISTS `skills` (`player` TEXT PRIMARY KEY, `strength` INTEGER, `dexterity` INTEGER, `defence` INTEGER)");

    Getting exp value:
    Code:
    public int getExp(Player player)
        {
            String name = player.getName();
            ResultSet result = db.query("SELECT `exp` FROM `levels` WHERE `player` = '" + name + "'");
         
            int exp = db.resultInt(result, 1);
         
            return exp;
        }
    I also looked to your Db.class and it looks ok. I have no idea what's wrong, I'm fighting with that problem over 10 hours. I just tried everything I know. I used SQLite Manager to check is query syntax corrent, well it is. Now I have no idea what to do.


    # Edit
    And yes, I checked NPE twice and it is on SELECT query line.
     
  6. Yes but where you're calling that db = new Db() code ? You should be calling it somewhere in the constructor otherwise you risk of calling getExp() before db is asigned.

    Also, you should use plugin.getDataFolder() instead of hardcoding the plugin path.
     
  7. Offline

    Chlorek

    1. new Db() is in here:
    Code:
    public CLRPGListeners(CLRPG plugin)
        {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
    I changed to onEnable() and nothing changes.
    2. plugin.getDataFolder() caused some problems (idk why, should work fine, only affects on my minecraft hosting, localhost works fine with that)

    As I said before, all queries work fine, this one does not only.

    Nobody can help? Please, it is very important for me.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  8. Offline

    rjVapes

    You said the new Db is in there, but it's not shown in the code you pasted, is it in the CLRPGListeners constructor, just not shown?

    Is db a member of CLRPGListeners?

    Is GetExp a member of CLRPGListeners?
     
  9. Offline

    Chlorek

    Well, I changed lots of things (I am still trying to make it working). Maybe I will post my code:
    Code:
    import java.io.File;
    import java.sql.ResultSet;
     
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class CLRPGFunctions extends JavaPlugin
    {
        Db db = null;
       
        public void openDb()
        {
            db = new Db(this, "plugins" + File.separator +
                    "clRPG" + File.separator +
                    "database.db");
     
            db.query("CREATE TABLE IF NOT EXISTS `levels` (`player` TEXT PRIMARY KEY, `exp` INTEGER)");
            db.query("CREATE TABLE IF NOT EXISTS `skills` (`player` TEXT PRIMARY KEY, `strength` INTEGER, `dexterity` INTEGER, `defence` INTEGER)");
        }
       
        public int getExp(Player player)
        {
            String name = player.getName();
           
            if(db != null)
            {
                ResultSet result = db.query("SELECT `exp` FROM `stats` WHERE `player` = '" + name + "'");
               
                int exp = db.resultInt(result, 1);
               
                return exp;
            }
            else
            {
                openDb();
            }
           
            return -1;
        }
       
        public void addExp(Player player, int exp)
        {
            String nick = player.getName();
            int newExp = getExp(player)+exp;
            if(db != null)
            {
                db.query("INSERT INTO `levels`(`player`,`exp`) VALUES('" + nick + "', " + newExp + ")");
            }
            else
            {
                openDb();
            }
        }
       
        public int getStrength(Player player)
        {
            return 0;
        }
       
        public int getDexterity(Player player)
        {
            return 0;
        }
    }
    I moved all my database functions to this class. So I am trying to make my code cleaner and that may show me my mistakes. However no effects yet ;/
     
  10. Offline

    rjVapes

    I'm not sure what DB stuff you're using but keep in mind creating a connection object is generally different than opening the actual connection.

    You want to open the connection, run a query, then close the connection. It's possible that the Db constructor opens a connection to test out your initialization stuff and then times out or something. See if there's an equivilant of db.open() and db.close(), and place them around your queries such that:
    Code:
    db.open();
    ResultSet result = db.query("SELECT `exp` FROM `stats` WHERE `player` = '" + name + "'");
    //... Any other queries in the same function
    db.close();
    //... parse results
     
  11. Chlorek
    Do you ever trigger openDb() ? Do you make more than one instance of that class ?
     
  12. Offline

    Chlorek

    @Up
    I trigger openDb() when:
    - plugin starts up
    - when I call some query (if db == null)

    Alright, I do not know what I did, but after changing right everything I could I made it "like working". Well I have not NPE error =]
    Now I have problem like this:
    Code:
    16:13:34 [SEVERE] Database result error: ResultSet closed
     
    16:13:34 [SEVERE] Database query error: [SQLITE_CONSTRAINT] Abort due to constraint violation (column player is not unique)
     
    16:13:41 [SEVERE] Database result error: ResultSet closed
    I understand that is it because of "column player is not unique", but what I have to do? I just created table:
    Code:
    db.query("CREATE TABLE IF NOT EXISTS `stats` (`player` TEXT PRIMARY KEY, `exp` INTEGER)");
    What's wrong?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  13. It's a SQL error, primary keys can have unique values, are you trying to insert players without checking if they're already there ?
     
  14. Offline

    Chlorek

    Oh I found some informations about this error. Now I do not know how to make "put" instead of INSERT, however update did not work.

    Also, UPDATE is not the best. If I do not put some things, it sets it to null.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
Thread Status:
Not open for further replies.

Share This Page