tags:

views:

469

answers:

4

I have a method ... where I can't find the error:

    public String getUsernameforID(int id) {
    String statment = "SELECT USERNAME  FROM `BENUTZER` WHERE `ID` = ? ;";
    String username = null;
    try {
        PreparedStatement ps = dbCommunicator.getStatment(statment);  // HERE : NULL POINTER EXECTION
        ps.setInt(1, id);
        ResultSet rs = dbCommunicator.readFromDB(ps);

        if (rs.first()) {
            username = rs.getString("USERNAME");
        }
    } catch (SQLException ex) {
        Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
    }
    return username;

I think it's the statement ... but how can I find this out? I get a Null Pointer Exeption.

Edit : my getStatment-method:

    public PreparedStatement getStatment(String st) {
    connect();
    PreparedStatement ps = null;
    try {
        ps = (PreparedStatement) connection.prepareStatement(st);
    } catch (SQLException ex) {
        Logger.getLogger(DBCommunicator.class.getName()).log(Level.SEVERE, null, ex);
    }
    return ps;
    }

The Exception:

Exception in thread "main" java.lang.NullPointerException
    at test.DBCommunicator.getStatment(DBCommunicator.java:107)
    at test.database.DBManager.getUsernameforID(DBManager.java:359)
    at dbtestdrive.Main.main(Main.java:25)
+1  A: 

It is probably because your query is failing, try changing it to:

String statment = "SELECT USERNAME  FROM `BENUTZER` WHERE `ID` = ?";

Your original query had a trailing semi-colon, which is illegal. This could have caused a nullpointerexception to be thrown up at a later stage, I would advise you to post the contents of your exception.

EDIT:

dbCommunicator.getStatment(statment);

Should be:

dbCommunicator.getStatement(statment);

You're misspelling 'statement' as 'statment', which is fine for a variable name, but not when referring to a method :)

karim79
no .. that was not the problem :(
darkrain
So paste your exception, or at least the line number that's throwing it
karim79
See my edit please
karim79
unfortunately its not the error :S
darkrain
A: 
//Ending ";" inside the string, is not neccessary.
String statment = "SELECT USERNAME  FROM `BENUTZER` WHERE `ID` = ? ;";

Instead of:

PreparedStatement ps = dbCommunicator.getStatment(statment);

PreparedStatement ps = dbCommunicator.prepareStatement(statment);

That's what I have using oracle.

Macarse
A: 

OWWw... i made a terrible Mistake ... i wasn't logged as root :S in the database... SOOORRY.. it can be closed :/

darkrain
Use something like:GRANT ALL ON db.* TO 'user'@'localhost' IDENTIFIED BY 'pass';instead of root access!
Macarse
I would call working as root a problem itself... Why would you ever do that except when granting rights to a specified user?
Fredrik
True, never make running as root a requirement for your applications. Its like calling a disaster.
Elitecoder
+1  A: 

Although this question may have been solved, here's a little bit on how to go about debugging given an stack trace.

In this case, the stack trace is the following:

Exception in thread "main" java.lang.NullPointerException
    at test.DBCommunicator.getStatment(DBCommunicator.java:107)
    at test.database.DBManager.getUsernameforID(DBManager.java:359)
    at dbtestdrive.Main.main(Main.java:25)

What this shows is that in the test.DBCommunicator.getStatment method, a NullPointerException was thrown at the location of line 107 of DBCommunicator.java.

Furthermore, the getStatment method was called from line 358 of DBManager.java which is in the DBManager.getUsernameforID method.

Therefore, the first place that should be checked is what is going on at line 107 of DBCommunicator.java.

Although there are no line numbers in the code snippet given, one can presume that a NullPointerException occurred in the following line:

ps = (PreparedStatement) connection.prepareStatement(st);

There's one thing that is fairly common about NullPointerExceptions -- they generally arise when method calls are performed on an null reference. Invoking a method on an object that doesn't exist will throw a NullPointerException.

In the above code, one can expect that the connection variable is actually null, rather than having a Connection.

This should lead to trying to track down why the connection variable is null rather than having a valid connection to the database that is initialized.

coobird