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)