15

Admittedly, my question is basically the same as this one, but it seems to have been left unanswered:

NullPointerException in handleStopActivity -- No reference to my code in stack trace

Downloaded Eclipse Helios, Android Developer Tools Plugin, and the JDK all within the last week. I was messing around with an app on my device, ran it in Debug mode, and it unexpectedly terminated. I realized I had caused a NullPointerException and the problem itself was not an issue for very long.

What is an issue, however, is that the debugger seems unable to identify where in my code the exception was thrown. The stack trace does not reference my code.

Indeed, if I put the following in the OnCreate() method, I get the same problem

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //lI("onCreate()");  //A silly logging thing I messed around with

    Integer iDareYou = null;
    iDareYou.byteValue();

Much to its credit, Eclipse certainly warns me that the code is likely to blow up in my face. But when I actually run this on my device, the stack trace returned is the following:

Thread [<1> main] (Suspended (exception RuntimeException))  
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2787  
ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2803   
ActivityThread.access$2300(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 135 
ActivityThread$H.handleMessage(Message) line: 2136  
ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
Looper.loop() line: 144 
ActivityThread.main(String[]) line: 4937    
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
Method.invoke(Object, Object...) line: 521  
ZygoteInit$MethodAndArgsCaller.run() line: 868  
ZygoteInit.main(String[]) line: 626 
NativeStart.main(String[]) line: not available [native method]  

I searched around for some answers and unfortunately all I could come up with were questions of the 'Why is my Android app causing a NullPointerException'? variety.

I'm hoping to find out what I should be doing differently in order to be able to find the point in my code where I caused the exception.... because I'm sure this ability would come in handy at some point in the future!


Going to edit this to add some information. As the first comment below mentions, the state in which the debugger suspends the thread is not the 'final' state of the application. Pressing 'resume' a couple times moves the process along to the point where the process actually terminates, although unfortunately no helpful stack trace is displayed in the Eclipse/ADT Debug window. However, at this point the stack trace I pasted above Followed By the first 3 unique lines of the stack trace of the NPE in my code is output to LogCat. I had previously looked to see if LogCat had anything I could use, but most likely it was before I had pressed 'resume' in the debugger to move the process to this state. Unfortunately though, doing it this way never displays a stack trace of the NPE in my code in the actual debugger.

But the news isnt all bad - from the information in the 'Variables' window on the right, I can see that there is an NPE in the thread's current scope, so that would at least give me a clue that an NPE is going on (and of course there is LogCat).

So based on a suggestion in a different topic, the best work around I've found is to add a "Caught and Uncaught" breakpoint for NullPointerException. When I do this and relaunch, the debugger displays the stack trace of the NPE in my code, and moves the edit window straight to the line where I caused the NPE.

At the moment I believe this is because the NPE is being caught (and presumably, thrown again) by the Android framework. It appears the original dilemma was caused by the fact that the Eclipse/ADT/DalvikVM Debug Stack Trace window does not include the 'caused by' information (as far as I can tell) that is displayed in the stack trace "later" appearing in LogCat. I will investigate further as to whether this can be remedied :)

I appreciate Bert F's suggestion, as I was not fully aware that the thread was not yet in its 'final' state. While it is true that the stack trace is [later] displayed in LogCat, I did not 'miss' it in LogCat because it was not [yet] there, what I did miss was hitting 'resume' which would have caused the stack trace to be output in LogCat.

Even though I originally got the debugger to stop at the line in my code using a breakpoint, there is a case for the fact that I could have ultimately obtained the stack trace in LogCat after resuming the thread, which would have effectively solved the problem. The only other thing I could really ask for would be to have the debugger stop at the line in my code causing the exception, instead of it being caught and later re-thrown by the Android framework. But I believe that would be a different question :) And Eclipse, being useful as it is, will send me there if I click on the appropriate line in the stack trace displayed in the LogCat window.

Community
  • 1
  • 1
user602347
  • 155
  • 1
  • 4
  • 2
    It looks like the program suspended in the debugger and did not continue printing the exception and full stack trace. Letting the program continue and then showing the full exception message and full stacktrace would be useful. Often an NPE is wrapped by multiple exceptions (printing "caused by" in the stack trace) - the lowest "caused by" message is probably the most important. Also, if you know your rev, it may help to find the source to match to the stack trace. – Bert F Feb 03 '11 at 22:58
  • Bert, if you wish to re-post as an answer I am willing to accept it, one could say 'pushing resume will get the stack trace I asked for' – user602347 Feb 04 '11 at 06:57
  • You're not the only person who couldn't understand this! You're quite right though - hit play a couple of times to let the exception bubble up, and then look at LogCat instead of the stack trace. I've used many, many IDEs and it seems bizarre to me that "which line of my code went wrong" would appear to be something that only experts can discover... – Chris Rae Nov 16 '12 at 02:38
  • Have you tried IntelliJ? Maybe the debugger in that works better. – Torp Jun 03 '13 at 03:53

4 Answers4

3

I ran your exact code and LogCat gave me the following exceptions:

Caused by: java.lang.NullPointerException
    at mypackage.test.TestActivity.onCreate(TestActivity.java:23)

Of course it is the

iDareYou.byteValue();

statement. This is clear as a bright day!

I suggest that you must learn how to use Dalvik Debug Monitor Server in the proper way.

The log you posted seems pretty useless, did you messed up some of the LogCat filters(it happened to me once)?

OFFTOPIC: "Where in my code did I cause the exception?" - I think that your question is not well formulated.

I think that proper name of this question might be - 'How to use Android debugger and LogCat? Some exceptions are not logged properly!'

Kiril Kirilov
  • 11,167
  • 5
  • 49
  • 74
  • 1
    Thanks for your opinions. I believe the question name was appropriate because I was not viewing the stack trace with LogCat, I was using the Debug window in Eclipse/ADT which I did state in the question name. You are suggesting that I can solve the original problem with LogCat, which is certainly true, however my question was about why the Eclipse/ADT window appeared to not display any helpful stack trace. – user602347 Feb 04 '11 at 06:22
  • 1
    Also the stack trace was not displayed in LogCat at the time the debugger suspends the thread, so the answer was not as simple as my failing to properly interpret what is shown in LogCat. – user602347 Feb 04 '11 at 07:01
1

iDareYou.byteValue();

Calling this on a Null-Value is causing the problem if you want to convert I recommend trying out this: Сonverting int to byte in android

Community
  • 1
  • 1
Nickolaus
  • 4,785
  • 4
  • 38
  • 60
1

In Eclipse the Android SDK provides several views. On of it is called "LogCat". There you can find the exception stack traces

Nikhil
  • 16,194
  • 20
  • 64
  • 81
Inumeg
  • 11
  • 1
0

In my experience, Android eats up the exceptions caused when inflating a layout. It should certainly report the exception, but apparently some Android developer sadly decided to catch it and create a new one -- without adding the former as cause. I view it as a deficiency of the Android environment; as it can be hard to reach the Android team I have lived with it until now, but I would report it to Google if it really bothered me.

alexfernandez
  • 1,938
  • 3
  • 19
  • 30