14

I got problem with Android. I develop on device and have problem with catching exceptions. I'm running some code in AsyncTask and finally simplified it to:

try
{
    if (true)
        throw new Exception("ERROR"); 
}      
catch (Exception e)
{    
    Log.e("e", "exception", e);
}

My problem is that 'e' variable is always null. Not sure what's happening actually. What's more it sometimes works, but I can't say when. I just get up from computer for few minutes come back and boom, it works. Doing coding few minutes and again it's null... There was one question on SO about 1 year ago but noone known answer. Maybe this time someone will have some idea.

Null exception

I think that it have something to do with AsyncTask as outside of it, I got exception catched properly... still don't have any clue why :( I found it only happens when debbuger is connected. When I take out cable from device it actually catches and exception isn't null anymore...

mariozski
  • 1,134
  • 1
  • 7
  • 20
  • Are you instantiating the AsyncTask on the UI thread? Are you invoking the execute(Params...) on the UI thread? Are you calling onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...) manually? Are you callong the task more than one time? – Italo Borssatto Jul 28 '11 at 19:24
  • One instance, no manual calls to on* methods. Invoking it with execute(params) on UI thread. I think I'll just pass on that case... thing is other people get it working so I'll stay for now with developing on my vm linux box heh – mariozski Jul 28 '11 at 23:07
  • Wise decision! If it's working in the cell phone, no problem. – Italo Borssatto Jul 29 '11 at 14:07
  • 1
    Hello! I have same problem, and I can reproduce it on other computers. I found this interesting http://www.adarshr.com/papers/npe – Pavel Vyazankin Sep 03 '13 at 20:04
  • I just had this problem as well with an `AsyncTask`. I tried to open an `OutputFileStream` to a file whose parent directory didn't exist yet. This just crashed with a null exception. In my case, adding `parentDir.mkdirs()` fixed the issue, but that is probably a clue as to what is going on. – Liron Oct 25 '13 at 08:41
  • I'm also getting this, only when the debugger is connected. – Evan Knowles Feb 19 '14 at 12:34
  • There are many responses here which point to the debugger. What if I told you I am seeing this in the wild? `try { /* a bunch of stuff */ } catch (IOException e) { Log.e(tag, e.getMessage()); } // app crashes with NPE on Log.e.` – QED Apr 23 '16 at 20:04

4 Answers4

4

It's impossible to have e with null value at that point. If you are debugging your app using Eclipse, it will show the e.toString() value at that point and the e.toString() is returning null.

Try another test, using this code:

try {
    if (true) {
        throw new Exception("ERROR");
    } 
}      
catch (Exception e) {
    if (e == null) {
        Log.e("e", "e is really null!!!");
    }
    else {
        Log.e("e", "e is not null, toString is " + e + " and message is " + e.getMessage());
    }
}
Italo Borssatto
  • 15,044
  • 7
  • 62
  • 88
  • 4
    I think you don't undestand... variable e is null... you can't execute method on it as you'll get NullPointerException. Other than that 3rd parameter of Log.e is Throwable not String... – mariozski Jul 26 '11 at 17:52
  • Well I tried but then can't compile project :) "The method e(String, String, Throwable) in the type Log is not applicable for the arguments (String, String, String)" – mariozski Jul 26 '11 at 17:55
  • What I'm trying to tell is that the e is not null, but the e.toString() is returning null. I changed my answer. Try that to test if I'm right. I have already spent a lot of time with something like that in some MyBatis exceptions. – Italo Borssatto Jul 26 '11 at 17:59
  • Hey, wasn't near pc. I'll try it tomorrow as I went home already :) But when I debug it 'e' var shows as null so I'm not sure if it'll actually help but will let you know tomorrow. – mariozski Jul 26 '11 at 19:17
  • That doesn't help. I've made screenshot to show what I'm talking about. It's not about logging issue it's about that exception is null for some unknown reason. When I try to do any method on 'e' my app crash and got NullPointerException as one can expect. – mariozski Jul 27 '11 at 09:45
  • Well.. it's really null as I've been using it's value to show message in other place and it didn't show up... – mariozski Jul 28 '11 at 07:42
  • 1
    Unbelievable! I've never seen something like that and, if it's happening, it's a bug in Android. Is there any other information that you missed? – Italo Borssatto Jul 28 '11 at 12:17
  • Actually I didn't see something like that either until now :) Tried 3 different devices, 2 windows instances and 2 different eclipse & sdk version... still same. I installed linux on virtual machine and it seems to be working there. Same code works for my co-workers aswell... I think that must be some kind of hardware problem but who knows... – mariozski Jul 28 '11 at 17:56
  • 2
    Got the same problem with Java 7 after re-throwing an exception. – Marcello DeSales May 03 '13 at 15:48
  • the problem occurred also for me: http://stackoverflow.com/questions/8400711/java-exception-itself-is-null/30105474#30105474 – ceph3us May 07 '15 at 16:41
2

I know this is an old question, but this happened with me as well, and it seems like the problem is with the debugger itself!
When I run my app from eclipse (ctrl + F11), it catches a proper exception (e != null).

Vitor M. Barbosa
  • 3,286
  • 1
  • 24
  • 36
2

I had this same problem.... I found it was the debugger like the other people had said. So what I did to debug is to just put the break point on the first line of the catch block, rather than right on the catch block. Seemed to work for me!

  • 1
    The weirdest thing, i've never seen it before. I tried all the recommendations on this page, a couple of times, but none worked. finally after doing them each a few times, this one finally succeeded and populating the exception for no apparent reason. Guess you have to be persistent – JCricket Feb 11 '15 at 14:19
  • Had the same problem and this fixed it. This should be the accepted answer. And this bug should be reported to JetBrains IntelliJ. – John Oct 23 '15 at 15:31
1

I had exactly the same strange problem inside an AsyncTask, while debugging on a real device (Galaxy Tab 2). (And yes, I got "e is really null!!!" doing the test suggested by @italo)

For me the problem mysteriously went away after unplugging the usb plug of the android device and connecting it again afterwards (and then running my app again).

Another suggestion, cleaning and rebuilding the project, as explained here didn't solve it for me (but maybe for somebody else).

Community
  • 1
  • 1
yonojoy
  • 5,486
  • 1
  • 31
  • 60