Java try catch 块中的空指针异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21288433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:54:49  来源:igfitidea点击:

Null Pointer exception within a try catch block

javaandroidnullpointerexception

提问by Bachalo

Getting the following runtime error, causing my application to crash on launch

出现以下运行时错误,导致我的应用程序在启动时崩溃

E FATAL EXCEPTION: MonitoringThread 13533 AndroidRuntime E Process: foo.com, PID: 13533 13533 AndroidRuntime E java.lang.NullPointerException 13533 AndroidRuntime E at foo.com$MonitoringThread.run(foo.java:125) 13533
AndroidRuntime E at java.lang.Thread.run(Thread.java:841)

E 致命异常:MonitoringThread 13533 AndroidRuntime E 进程:foo.com,PID:13533 13533 AndroidRuntime E java.lang.NullPointerException 13533 AndroidRuntime E at foo.com$MonitoringThread.run(foo.java:125) 13533
E java.AndroidRuntime .Thread.run(Thread.java:841)

The offending line is

违规行是

ret = mConnection.getInputStream().read(buffer);

in the following code snippet

在下面的代码片段中

try {
    ret = mConnection.getInputStream().read(buffer);
    } catch (IOException e) {
    break;
    }

Can anyone suggest next steps in trying to debug? I thought that use of a try catch block would eliminate any null pointer errors.

任何人都可以建议尝试调试的后续步骤吗?我认为使用 try catch 块可以消除任何空指针错误。

采纳答案by marcinj

You should not use try / catch blocks to eliminate null pointer exceptions. Null pointer exceptions should be passed down, to let programmer know that problem arises and where.

您不应该使用 try / catch 块来消除空指针异常。应该传递空指针异常,让程序员知道问题出现在哪里。

In your case, you are catching IOException, so its not NullPointerException.

在您的情况下,您正在捕获 IOException,因此它不是 NullPointerException。

Also check what is null that is causing this exception, maybe its mConnection ? or getInputStream() returns null.

还要检查导致此异常的 null 是什么,也许是它的 mConnection ?或 getInputStream() 返回 null。

From this example, you can also see that its best to not execute lots of methods in one line:

从这个例子中,你还可以看到最好不要在一行中执行很多方法:

ret = mConnection.getInputStream().read(buffer);

its better to write:

最好这样写:

InputStream is = mConnection.getInputStream();
ret = is.read(buffer);

this way you will know from callstack where NPE originated,

这样你就可以从调用堆栈中知道 NPE 的起源地,

if your code is unsafe, like you know you can get nullpointer from some method, then simply check it:

如果您的代码不安全,就像您知道可以从某些方法获取空指针一样,那么只需检查它:

InputStream is=null;
if ( mConnection != null ) {
   is = mConnection.getInputStream();
   if ( is != null ) {
     ret = is.read(buffer);
   }
   else {
      // log error?
   }
} 
else {
   // log error?
}

回答by Mike B

There are a couple possibilities. Either mConnectionis null, getInputStream()returns null, or bufferis nulland the read()method is throwing the NPE. My best guess would be mConnectionis null, that's where I'd start.

有几种可能性。要么mConnectionis nullgetInputStream()returns null,要么bufferisnull并且该read()方法正在抛出 NPE。我最好的猜测mConnectionnull,这就是我要开始的地方。

回答by Umer Farooq

try {
    ret = mConnection.getInputStream().read(buffer);
    } catch (Exception e) {

     Log.e("your app", e.toString());
     break;
    }

Should solve the issue

应该解决问题

回答by java seeker

try {
    ret = mConnection.getInputStream().read(buffer);
    } catch (IOException e) {
    break;
    }

break ret = mConnection.getInputStream().read(buffer); this statement

中断 ret = mConnection.getInputStream().read(buffer); 这个说法

 try 
 {

   if(mConnection!=null)
   {
     InputStream reader=mConnection.getInputStream();
     if(reader!=null)ret= reader.read(buffer);
   }
 } catch (IOException e) 
 {
        break;
 }