1

N.B. Others have asked this question and gotten flagged as duplicate. When I looked at the "answers" to the duplicates none of them seemed to actually answer the question, with some posters denying that it's even possible for this condition to exist. So before you flag this as duplicate look at your proposed duplicate answer to see if it actually does answer the question.

Examples:

Java: Exception itself is null

How is it possible that the Exception is null inside of a catch block?

I'm debugging an Android program, running on an actual device (i.e., not in emulation) using Eclipse (Indigo). When I execute this code here . . .

       try  {
           bCheckState = confirmed.get(position);
       }
       catch(Exception e)  {
           Log.e ("MyListActivity.confirmed.get(position) Crash", "details " + e);
           int confirmedCount = confirmed.size();  // for debugging
       }

... it lands in the catch block and e is null. I don't think the debugger and source code are out of sync because when I step from the logging line to the next line that's when the log entry appears in the log. And when I step through the next line confirmedCount changes appropriately. The log entry says:

05-13 11:55:54.966: E/MyListActivity.confirmed.get(position) Crash(11491): details null

... which seems to suggest it really is null.

So what's going on here?

Community
  • 1
  • 1
user316117
  • 7,971
  • 20
  • 83
  • 158
  • very strange indeed. Try to restart eclipse OR refresh the project sources and do a project clean on your eclipse. – Alécio Carvalho May 14 '14 at 16:17
  • Try e.getMessage().toString() – user3455363 May 14 '14 at 16:19
  • 1
    Lots of rebuilds/cleans. confirmed is not null. Since the log is already showing e as null why would e.getMessage().toString() be any different? – user316117 May 14 '14 at 16:24
  • Just for yucks I tried the e.getMessage().toString() suggestion. Predictably the program crashed with a NullPointerException – user316117 May 14 '14 at 16:34
  • Can you reproduce this (the log message saying it is null) when running *without* the debugger? And yes, this appears to have already been covered in the other questions, particularly Stephen C's answer (technically, duplication is about problems, not about answers) – Chris Stratton May 14 '14 at 17:00
  • 1
    @Chris Stratton When I run it outside the debugger the log shows a "thread exiting with uncaught exception" issued by dalvikvm, followed by "FATAL Exception: main" issued by AndroidRuntime. and then an IndexOutOfBoundsException, which I think is the original exception I was investigating (the position index is too big) I do not see my log entry in the list. – user316117 May 14 '14 at 17:14
  • @Der Golem when I comment out that line it makes no difference. What was your idea with that? – user316117 May 14 '14 at 17:15
  • So without the debugger you never even hit the catch block? It sounds very much like your debugger is distorting the program execution, as discussed in the other questions. I'd recommend you solve the actual problem with the program using the log messages and stack trace - breakpoint debuggers are rarely actually *necessary*. Though if you can come up with concise code to trigger the issue, you could file a bug against the debugger or VM. – Chris Stratton May 14 '14 at 17:16
  • @Der Golem But I already said confirmed is not null. I can examine it and its contents in the debugger just fine. Plus if confirmed was null in the try block then e would signal a NullPointerException; e itself would not be null. – user316117 May 14 '14 at 17:22
  • @Chris Stratton - when running outside the debugger on a physical device how do I get a stack trace? – user316117 May 14 '14 at 18:06
  • Connect the ADB cable and run the logcat tool from the command line, or let eclipse do it. logcat and the JDWP debugger are independent tools, though both of course rely on the ADB communication channel. Normally, you can "run" your app rather than "debug" it, and if eclipse/adt is open it will show the logcat messages. – Chris Stratton May 14 '14 at 18:09
  • @Chris Stratton I'm already doing that and all I see in Logcat are log messages. How do I see a stack trace? – user316117 May 14 '14 at 18:15
  • 1
    You will see the stack trace in the log if an exception occurs, unless you have a handler without e.printStackTrace(), in which case you may silence the failure. – Chris Stratton May 14 '14 at 18:20
  • @Chris Stratton I must have that, then. Where/how is that handler set or configured? – user316117 May 14 '14 at 18:24
  • Make sure each of your catch blocks has an e.printStackTrace() in it. In the case of something which you don't handle, which instead crashes the app, the system should already be printing the stack trace to the log. – Chris Stratton May 14 '14 at 18:43

0 Answers0