我有一个方法......在那里我找不到错误:
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;
我认为这是该语句......但我怎么能发现这一点? 我得到一个空指针Exeption。
编辑:我getStatment法:
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;
}
例外:
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)