为什么我从这个Java代码中获得Null Pointer Exception?

时间:2022-12-24 16:48:35

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.

我认为这是声明......但我怎么能找到这个呢?我得到一个Null Pointer Exeption。

Edit : my getStatment-method:

编辑:我的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;
    }

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)

3 个解决方案

#1


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.

这表明在test.DBCommunicator.getStatment方法中,在DBCommunicator.java的第107行的位置抛出了NullPointerException。

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

此外,从DBManager.java的第358行调用getStatment方法,该行位于DBManager.getUsernameforID方法中。

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

因此,应该检查的第一个位置是DBCommunicator.java第107行的内容。

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

虽然给出的代码片段中没有行号,但可以假设在以下行中发生了NullPointerException:

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.

NullPointerExceptions有一个相当常见的事情 - 它们通常在对null引用执行方法调用时出现。在不存在的对象上调用方法将抛出NullPointerException。

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

在上面的代码中,可以预期连接变量实际上是null,而不是具有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.

这应该导致尝试跟踪连接变量为空的原因,而不是与初始化的数据库建立有效连接。

#2


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.

您的原始查询有一个尾随的分号,这是非法的。这可能导致在稍后阶段抛出nullpointerexception,我建议你发布你的异常内容。

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 :)

你错误拼写'声明'为'statment',这对于变量名称很好,但在引用方法时却没有:)

#3


//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.

这就是我使用oracle的原因。

#1


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.

这表明在test.DBCommunicator.getStatment方法中,在DBCommunicator.java的第107行的位置抛出了NullPointerException。

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

此外,从DBManager.java的第358行调用getStatment方法,该行位于DBManager.getUsernameforID方法中。

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

因此,应该检查的第一个位置是DBCommunicator.java第107行的内容。

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

虽然给出的代码片段中没有行号,但可以假设在以下行中发生了NullPointerException:

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.

NullPointerExceptions有一个相当常见的事情 - 它们通常在对null引用执行方法调用时出现。在不存在的对象上调用方法将抛出NullPointerException。

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

在上面的代码中,可以预期连接变量实际上是null,而不是具有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.

这应该导致尝试跟踪连接变量为空的原因,而不是与初始化的数据库建立有效连接。

#2


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.

您的原始查询有一个尾随的分号,这是非法的。这可能导致在稍后阶段抛出nullpointerexception,我建议你发布你的异常内容。

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 :)

你错误拼写'声明'为'statment',这对于变量名称很好,但在引用方法时却没有:)

#3


//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.

这就是我使用oracle的原因。