0

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.

5
  • 3
    Which is line 37? Why doesn't the output match with the code? And why do you return null when you get an exception, instead of simply let it bubble up the stack. A clear SQLException containing the SQL error message is much easier to analyze than an obscure NullPointerException. Stop catching Exception and returning null or worse: completely ignoring the exception. Commented Mar 9, 2014 at 11:59
  • 1
    If you are logging already you might want to log the exception being thrown in checkQuery as well :) Commented Mar 9, 2014 at 11:59
  • @JBNizet I have edited the code to display the SQLExceptions, however none are being thrown. I did this in both runQuery and checkQuery. Commented Mar 9, 2014 at 12:07
  • 2
    No way. executeQuery() returns a non-null result set, or throws a SQLException. You're doing something wrong. Commented Mar 9, 2014 at 12:07
  • Where do you create the statement object? Most likely that is null. Commented Mar 9, 2014 at 15:34

2 Answers 2

1

You have a syntax error on table name. Table names should not be a literal but should be quoted with back ticks. These back ticks are optional unless table name has any special chars in it or if it is a reserved word.

Just because of this error, the statement

return statement.executeQuery(query);

is causing an Exception and the method was returning a null for ResultSet.
You better catch the exception and see what the stacktrace says on it.

Change:

QLManager.runQuery("select * from \'accounts\'");

To:

QLManager.runQuery("select * from `accounts`");
Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't really matter, as I've done it both ways using JDBC, however I changed it just for kicks and the error persists.
No. The statement return statement.executeQuery(query); sure have returned a null due to the syntax error. Catch the exception and see its message.
1

You problem in this code:

if(rs == null) {
        Misc.error("Result Set returned null.");

if(rs.next())
        Misc.log("Current Row: " + rs.getRow());

In case any exception occurred in checkQuery method, it will return a null for ResultSet, then the code will proceed to rs.next(), which rs null, then a NullPointerException will raise.

What all you have to do is:

if(rs == null) {
    Misc.error("Result Set returned null.");
    return false;
}

if(rs.next()) {
    Misc.log("Current Row: " + rs.getRow());
    return true;
}

But you have to at least to log the error or throw the exception in checkQuery to figure out what is the exact problem that you are facing. not just return null object.

2 Comments

My question is more of what's causing the NullPointer, not how to avoid it. The if(rs == null) check was added to make sure that it was the cause.
What's causing the error is that in checkQuery(), you're getting a clear SQLException containing the explanation of the problem, but you catch it, ignore it, and return null. So the calling method ends up with a null resultset. checkQuery() should not catch any exception.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.