How can I find out where the Null Pointer exception came from?

I am having a problem with the Blackberry app. I found a guy who did a textbook that did what I tried with mine. I copied the sample application code to try to recreate it, just to see it in action. Result: Null Pointer exception.

I want to know what causes this. How can I?

+3
source share
7 answers

You can view stacktrace if you catch Throwable instead of catch Exception or any subclass of Exception. eg.

try
{
   //some code
}
catch(Throwable t)
{
    //Will automatically show a stacktrace in eclipse.  
    //I believe on a real device it will put the stacktrace in the eventlog.  
}
+3
source

BlackBerry , Debug, break, .

try {
    // Code that throws an exception
} catch (Exception e) {
    e.printStackTrace();
}

BlackBerry,

System.out.println(e.getMessage());

, , , println.

StackTrace, this:

LGLG . . .

, - RIM, - .

+2

, , , , .

catch, Throwable Exception. .

http://supportforums.blackberry.com/t5/Java-Development/How-to-get-stacktrace-from-a-real-device/m-p/27668

, Blackberry, , , printStackTrace.

+2

NPE . .

1. .

String temp = null; temp.length();

  1. eclipse.
  2. , eclipse temp.length(); .
  3. .
0

, , , , :

java.lang.NullPointerException
    at your.packege.ClassName.methodName(ClassName.java:169)

, .

try {..} catch (Exception ex) {..}

, .

, getStackTrace(), StackTraceElement

-1

.

, - :

try {
    // Code that throws an exception
} catch (Exception e) {
    e.printStackTrace();
}

Note. This should NEVER appear in production code!

-1
source

Look at the stack trace when the program crashes. It will tell you where (and usually which line of code, if available) the exception occurred, and also which object was null.

If you don't see the stack trace, combine everything in your main method using OMG Unicorns .

-1
source

Source: https://habr.com/ru/post/1740967/


All Articles