NullPointerException when trying to open SQLite connection

53 Views Asked by At

I'm relatively new to Java coding so bear with me here. I'm trying to create an Economy plugin for my Minecraft server that stores balances to an SQLite database. However, when I go to call any of the commands, I'm getting an NPE in my code. What stumps me about this is that it directs me back to where I'm defining a variable at the very beginning of a method I've set up to retrieve an SQL connection. I've looked absolutely everywhere on Google and through my own self-debugging and I can't for the life of me figure out what's wrong.

Here's the stack trace, including the first line that shows I execute the command /money. The stack trace points to line 44 in SQLite.java, which is File dataFolder = new File(plugin.getDataFolder(), dbname+".db");

[13:35:42] [Server thread/INFO]: Hawkzy issued server command: /money
[13:35:42] [Server thread/ERROR]: null
org.bukkit.command.CommandException: Unhandled exception executing command 'money' in plugin HawkzehEco v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
        at org.bukkit.craftbukkit.v1_8_R2.CraftServer.dispatchCommand(CraftServer.java:646) ~[spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.PlayerConnection.handleCommand(PlayerConnection.java:1139) [spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java:974) [spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) [?:1.7.0_75]
        at java.util.concurrent.FutureTask.run(FutureTask.java:262) [?:1.7.0_75]
        at net.minecraft.server.v1_8_R2.SystemUtils.a(SourceFile:60) [spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:712) [spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:368) [spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:651) [spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:554) [spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
        at java.lang.Thread.run(Thread.java:745) [?:1.7.0_75]
Caused by: java.lang.NullPointerException
        at me.hawkzeh.eco.SQLite.getSQLConnection(SQLite.java:44) ~[?:?]
        at me.hawkzeh.eco.SQLite.getBalance(SQLite.java:86) ~[?:?]
        at me.hawkzeh.eco.Commandmoney.onCommand(Commandmoney.java:15) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
        ... 15 more

SQLite.java

package me.hawkzeh.eco;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;

public class SQLite {
    String dbname;
    Main plugin;
    Connection connection;
    public double balance = 0;
    public String table = "balances";
    public SQLite(Main instance) {
        plugin = instance;
        dbname = "balances";
    }

    public SQLite() {

    }

    public String createBalancesTable =
    "CREATE TABLE IF NOT EXISTS balances (" +
    "`player` varchar(32) NOT NULL," +
    "`balance` double(16) NOT NULL," +
    "PRIMARY KEY (`player`)"+");";


    public void close(PreparedStatement ps, ResultSet rs) {
        try {
            if(ps!=null)
                ps.close();
            if(rs!=null)
                rs.close();
        } catch (SQLException ex) { SQLiteErrorHandler.close(plugin, ex); }
    }
    public Connection getSQLConnection() {
        File dataFolder = new File(plugin.getDataFolder(), dbname+".db");
        if(!dataFolder.exists()) {
            try {
                dataFolder.createNewFile();
            } catch (IOException e) { plugin.getLogger().log(Level.SEVERE, "File write error: "+dbname+".db"); }
        }
        try {
            if(connection!=null&&!connection.isClosed()) {return connection;}
        Class.forName("org.sqlite.JDBC");
        connection = DriverManager.getConnection("jdbc:sqlite:"+dataFolder);
        return connection; } 
        catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE,"SQLite exception on initialize", ex); }
        catch (ClassNotFoundException ex) { plugin.getLogger().log(Level.SEVERE, "You need the SQLite JBDC library. Google it. Put it in /lib folder."); }
        return null;
    }
    public void initialize() {
        connection = getSQLConnection();
        try {
            PreparedStatement ps = connection.prepareStatement("SELECT * FROM "+table+" WHERE player = ?");
            ResultSet rs = ps.executeQuery();
            close(ps,rs); }
        catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, "Unable to retrieve connection", ex); }
    }

    public void load() {
        connection = getSQLConnection();     
        try {
            Statement s = connection.createStatement();
            s.executeUpdate(createBalancesTable);
            s.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        initialize();
    }

    @SuppressWarnings("resource")
    public Double getBalance(String s) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = getSQLConnection();
            ps = conn.prepareStatement("SELECT * FROM "+table+" WHERE player = '"+s+"';");
            rs = ps.executeQuery();
            while(rs.next()) {
                if(rs.getString("player").equalsIgnoreCase(s.toLowerCase())) {
                    return rs.getDouble("balance"); 
                }
            }
        } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, SQLiteErrorHandler.sqlConnectionExecute(), ex); }
        finally {
            try {
                if(ps!=null)
                    ps.close();
                if(conn!=null)
                    conn.close();
            } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, SQLiteErrorHandler.sqlConnectionClose(), ex); }
        }
        return 0.0;
    }

    public void setBalance(String s, Double a) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = getSQLConnection();
            ps = conn.prepareStatement("REPLACE INTO "+table+" (player,balance) VALUES (?,?)");
            ps.setString(1, s.toLowerCase());
            ps.setDouble(2, balance);
            ps.executeUpdate();
            return;
        } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, SQLiteErrorHandler.sqlConnectionExecute(), ex); }
        finally { try {
            if(ps!=null)
                ps.close();
            if(conn!=null)
                conn.close();
        } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, SQLiteErrorHandler.sqlConnectionClose(), ex); }
        } return;
    }
}

And finally, here's my Commandmoney.java..

package me.hawkzeh.eco;

import net.md_5.bungee.api.ChatColor;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

public class Commandmoney implements CommandExecutor {

    @Override
    public boolean onCommand(CommandSender cs, Command cmd, String s, String[] args) {
        if(s.equalsIgnoreCase("money")) {
            SQLite sql = new SQLite();
            double amount = sql.getBalance(cs.getName());
            cs.sendMessage(ChatColor.AQUA+"Balance: "+ChatColor.GREEN+"$"+amount);
            return true;
        }
        return false;
    }
}
0

There are 0 best solutions below