Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Generic Null Pointer Exceptions

I have been programming for Android with decent success, but one thing I have noticed is that when I get NullPointerExceptions the stack trace is worthless. They are always of this format:

Thread [<1> main] (Suspended (exception NullPointerException))  
    ViewRoot.draw(boolean) line: 1431   
    ViewRoot.performTraversals() line: 1163 
    ViewRoot.handleMessage(Message) line: 1727  
    ViewRoot(Handler).dispatchMessage(Message) line: 99 
    Looper.loop() line: 123 
    ActivityThread.main(String[]) line: 4627    
    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]  

Usually stack traces tell you right where the code blew up, but I always seem to get the most generic one possible. Am I missing something obvious? Thanks.

like image 781
skaz Avatar asked Nov 08 '10 23:11

skaz


People also ask

How do I fix NullPointerException in Android?

How to fix the NullPointerException? To avoid NullPointerException we have to initialize the Textview component with the help of findviewbyid( ) method as shown below. The findViewbyId( ) takes the “id” value of the component as the parameter. This method helps locate the component present in the app.

How do I fix null pointer exceptions?

NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.

What is NullPointerException in Android?

NullPointerException is a runtime exception in Java that occurs when a variable is accessed which is not pointing to any object and refers to nothing or null. Since the NullPointerException is a runtime exception, it doesn't need to be caught and handled explicitly in application code.

Can NullPointerException be caught?

Programmers typically catch NullPointerException under three circumstances: The program contains a null pointer dereference. Catching the resulting exception was easier than fixing the underlying problem. The program explicitly throws a NullPointerException to signal an error condition.


1 Answers

If you are using Eclipse, look at LogCat in the DDMS or Debug perspectives. It will show you the stack trace of the exception itself and that of the code that caused it.

like image 65
Scooter Avatar answered Sep 27 '22 17:09

Scooter