3

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)

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.

5
  • Have you initialized mConnection? Commented Jan 22, 2014 at 16:26
  • no, a try catch block does not get rid of errors. I just gives you an opportunity to react on them Commented Jan 22, 2014 at 16:26
  • where is mConnection initialized? Commented Jan 22, 2014 at 16:26
  • @eco_bach you are catching only IOException !! Commented Jan 22, 2014 at 16:28
  • Also, when you catch an exception you should do something with it such as display a message, change a variable, etc... not just let it sit and stew Commented Jan 22, 2014 at 16:31

4 Answers 4

4

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.

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

Also check what is null that is causing this exception, maybe its mConnection ? or getInputStream() returns 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,

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?
}
Sign up to request clarification or add additional context in comments.

Comments

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

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

Should solve the issue

4 Comments

"Should solve the issue" - not really, because NullPointer will still be there... this is ignoring a problem, not solving it.
And do you know the purpose of Log.e()? That's there to tell what is the cause of Exception.
thanks, although the answer below by marcin_j is more detailed, this helps a great deal.
The break; is useless there @UmerFarooq
0

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

Comments

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

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

 try 
 {

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.