I am attempting at writing my own server as a personal project, however I'm running into some issues. I've finally completed the setup for a packet system between the Java server and the C# client which makes me very happy, even though I've had some help. Anyway, here's the code that I've written trying to get this to work properly. I created the SQLManager using static variables, because I read that the database connection should be static, if this is incorrect, please let me know.
Here's the error:
Exception in thread "main" java.lang.NullPointerException
com.fmeg.server.util.SQLManager.runQuery(SQLManager.java:37)
Here's my SQL Class:
public static boolean connectToDatabase() {
try {
connection = DriverManager.getConnection(host, credentials[0], credentials[1]);
connected = true;
} catch (Exception e) { connected = false; }
Misc.log("Database: " + database + " || Connection State: " + connected);
return connected;
}
public static boolean runQuery(String query) {
try {
ResultSet rs = checkQuery(query);
if(rs == null)
Misc.error("Result Set returned null.");
if(rs.next())
Misc.log("Current Row: " + rs.getRow());
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public static ResultSet checkQuery(String query) throws SQLException {
try {
Misc.log(query);
return statement.executeQuery(query);
} catch (Exception e) {
destroyConnection();
return null;
}
}
private static void destroyConnection() {
try {
statement.close();
connection.close();
connected = false;
Misc.error("Database connection destroyed!");
} catch (Exception e ) { }
}
Apparently, the ResultSet is returning null, here's the output in the console.
[LOG]: Database: Unity3D || Connection State: true
[LOG]: Server <Test Realm> Successfully Started on port: 9955!
[LOG]: select * from `accounts`
[ERROR]: Result Set returned null.
Here's where I'm calling the query:
SQLManager.runQuery("select * from \'accounts\'");
Any pointers would be greatly appreciated, as I don't exactly see what the problem is. To answer these questions if the do arise, yes I do have a table called accounts, and yes it does have entries.
checkQueryas well :)executeQuery()returns a non-null result set, or throws a SQLException. You're doing something wrong.statementobject? Most likely that isnull.