为什么我从此Java代码中得到一个Null Pointer异常? [英] Why am I getting a Null Pointer Exception from this Java code?
问题描述
我有一个方法...在哪里我找不到错误:
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;
我认为这是声明...但是如何找到答案? 我得到一个Null Pointer Exeption.
I think it's the statement ... but how can I find this out? I get a Null Pointer Exeption.
我的getStatment方法:
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;
}
例外:
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)
推荐答案
尽管这个问题可能已经解决,但是这里给出了一些有关如何在给定堆栈跟踪的情况下进行调试的信息.
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)
这表明在test.DBCommunicator.getStatment
方法中,在DBCommunicator.java
的第107行的位置抛出了NullPointerException
.
What this shows is that in the test.DBCommunicator.getStatment
method, a NullPointerException
was thrown at the location of line 107 of DBCommunicator.java
.
此外,getStatment
方法是在DBManager.getUsernameforID
方法中的DBManager.java
行358中调用的.
Furthermore, the getStatment
method was called from line 358 of DBManager.java
which is in the DBManager.getUsernameforID
method.
因此,首先要检查的地方是DBCommunicator.java
的第107行的情况.
Therefore, the first place that should be checked is what is going on at line 107 of DBCommunicator.java
.
尽管给出的代码段中没有行号,但是可以假定在下一行中出现了NullPointerException
:
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);
关于NullPointerException
的一件事很普遍-它们通常在对null
引用执行方法调用时出现.在不存在的对象上调用方法将抛出NullPointerException
.
There's one thing that is fairly common about NullPointerException
s -- 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
.
在上面的代码中,可以预期connection
变量实际上是null
,而不是Connection
.
In the above code, one can expect that the connection
variable is actually null
, rather than having a Connection
.
这应该导致尝试查找为什么connection
变量是null
的原因,而不是有效地连接到已初始化的数据库.
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.
这篇关于为什么我从此Java代码中得到一个Null Pointer异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!