Solved Null Point Exception while trying to grab int [MYSQL]

Discussion in 'Spigot Plugin Development' started by iMedia, Mar 12, 2020.

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

    Supporter

    Hey! Basically, I'm trying to grab an int from my SQL database. Unfortunately, I keep getting a Null Point Exception.

    getUserCoins method

    Code (Text):

    public static int getUserCoins(Player player) {
        try (Connection c = sql.openConnection()) {
            PreparedStatement statement = c.prepareStatement("SELECT coins FROM `playersdata` WHERE uuid=?;");
            statement.setString(1, String.valueOf(100));
            //ResultSet set = statement.executeQuery();
            ResultSet set = statement.getResultSet();
            set.next();
            return set.getInt("coins");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    Command (Very basic for now)

    Code (Text):

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        Player player = (Player) sender;
        int coins = AccountHandler.getUserCoins(player);
        player.sendMessage(String.valueOf(coins));
        return true;
    }
    Error

    Code (Text):

    Caused by: java.lang.NullPointerException
            at me.imedia.pvp.rank.AccountHandler.getUserCoins(AccountHandler.java:98) ~[?:?]
            at me.imedia.pvp.commands.Balance.onCommand(Balance.java:13) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            ... 15 more
    Database:

    upload_2020-3-12_20-39-37.png

    Can anyone help, please?
     
    • Friendly Friendly x 1
  2. Hello,

    First, your sql query while return no results, because the UUID is not found:
    statement.setString(1, String.valueOf(100)); (it's not even a valid uuid, and there is no UUID "100" in database?)

    Second, in your AccountHandler#getUserCoins() method:
    Code (Java):
    ResultSet set = statement.getResultSet();
    set.next();
    Statment#getResultSet javadoc:
    As there is no result, your set variable is null. Using #next() on the null "set" variable while throw a NullPointerException, not handled by your try block.
     
    • Agree Agree x 1
  3. iMedia

    Supporter

    Yeah I fixed that and the new code is:

    Code (Text):

    public static int getUserCoins(Player player) {
        try (Connection c = sql.openConnection()) {
            PreparedStatement statement = c.prepareStatement("SELECT coins FROM `playersdata` WHERE uuid=?;");
            statement.setString(1, Bukkit.getOfflinePlayer(String.valueOf(player)).getUniqueId().toString());
            //ResultSet set = statement.executeQuery();
            ResultSet set = statement.getResultSet();
            return set.getInt("coins");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
    However, the same error occurs. I'm not really sure why

    The error says it's to do with:
    Code (Text):

    return set.getInt("coins");
     
  4. It looks like you didn't properly read the previous reply.

    "Statment#getResultSet javadoc: return the current result as a ResultSet object or null if the result is an update count or there are no more results" (emphasis mine)

    Better use executeQuery() and
    Code (Text):
    if ResultSet#next:
        // get the thing
     
  5. iMedia

    Supporter

    I'm sorry for being so stupid. I've fixed all that now and I still get an error. It's always the null point exception.

    Here's the new code

    Code (Text):

    public static int getUserCoins(Player player) {
        try (Connection c = sql.openConnection()) {
            PreparedStatement statement = c.prepareStatement("SELECT coins FROM `playersdata` WHERE uuid=?;");
            statement.setString(1, Bukkit.getOfflinePlayer(String.valueOf(player)).getUniqueId().toString());
            ResultSet set = statement.executeQuery();
            if (set.next()) {
                return set.getInt("coins");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
     
     
Thread Status:
Not open for further replies.