空结果集异常

发布于 2024-12-07 23:38:25 字数 415 浏览 0 评论 0原文

我想连接到oracle并获取一些记录。

java.sql.ResultSet r = s.executeQuery("Select * from table1");
while (r.next()==true) {
    System.out.println(r.getString("column1").toString());  
      }

table1 包含像这样的行

  • row1 movieName1
  • row2 movieName2
  • row3
  • row4 movieName2

当我执行上述代码时,我收到“线程“main”java.lang.NullPointerException 中的异常”错误。我该如何解决它?

I want to connect to oracle and get some records.

java.sql.ResultSet r = s.executeQuery("Select * from table1");
while (r.next()==true) {
    System.out.println(r.getString("column1").toString());  
      }

table1 include like that rows

  • row1 movieName1
  • row2 movieName2
  • row3
  • row4 movieName2

I get "Exception in thread "main" java.lang.NullPointerException" error when I execute above code. How can I fix it ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

落花浅忆 2024-12-14 23:38:26
  1. r.getString("column1") 的返回值可能为 null,因此您将在后续的 toString() 上得到 NullPointerException 代码>方法调用。在使用返回值之前,您必须检查是否有 null 值。

  2. 您不需要在此处调用toString()getString 方法已返回一个字符串。

  1. The return value of r.getString("column1") might be null, so you will get a NullPointerException on the subsequent toString() method call. You have to check for null values before you use the return value.

  2. You don't need to to call toString() here. The getString method already returns a string.

原来是傀儡 2024-12-14 23:38:26

您没有说明异常发生在代码中的具体位置。最可能的原因是 snullr.getString("column1") 返回 null

You don't say where exactly in your code the exception occurs. The most likely causes are that s is null or that r.getString("column1") returns null.

溺ぐ爱和你が 2024-12-14 23:38:26

也许你会得到空指针,因为 row3 具有空值,并且你正在尝试“toString”-it

maybe you get nullpointer because row3 has null value and you're trying to "toString"-it

牵强ㄟ 2024-12-14 23:38:26

如果您发布的第一行抛出错误,我的猜测是变量 s 为 null(未初始化),但由于您没有确切地告诉我们代码的哪一行抛出错误,因此很难判断。

If its the first line that you posted that throws the error my guess would be that the variable s is null (not initialized), but since you don't tell us exactly what line of your code throws the error its hard to tell.

仅冇旳回忆 2024-12-14 23:38:25

嗯,看起来 column1 没有一行的值...因此 getString() 返回一个空引用,然后您调用 toString() on - 导致异常。当您调用 getString() 时,toString() 调用无论如何都是毫无意义的,因此您可以将其重写为:

while (r.next()) {
    System.out.println(r.getString("column1"));  
}

然后将打印 null反而。如果您想避免这种情况,请使用:

while (r.next()) {
    String name = r.getString("column1");
    if (name != null) {
        System.out.println(name);
    }
}

Well it looks like column1 doesn't have a value for one row... so getString() is returning a null reference, which you're then calling toString() on - leading to the exception. As you're calling getString() the toString() call is pointless anyway, so you could rewrite it as:

while (r.next()) {
    System.out.println(r.getString("column1"));  
}

That will then print null instead. If you want to avoid that, use:

while (r.next()) {
    String name = r.getString("column1");
    if (name != null) {
        System.out.println(name);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文