Christian Tucker
Christian Tucker

Reputation: 627

NullPointerError using JDBC

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.

Upvotes: 0

Views: 74

Answers (2)

Salah
Salah

Reputation: 8657

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.

Upvotes: 1

Ravinder Reddy
Ravinder Reddy

Reputation: 23982

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`");

Upvotes: 1

Related Questions