简体   繁体   中英

How can I know where a Null Pointer Exception originated?

I had a problem making a Blackberry app. I found some guy who made a tutorial which did what I attempted with mine. I copied the code of the tutorial's app to try and recreate it, just to see it in action. The result: Null Pointer Exception.

I want to know what is triggering this. How can I?

You can view the stacktrace if you catch Throwable instead of catching 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.  
}

There is no stack trace in the BlackBerry, the best is to use the Debug mode, so the application will break when the exception happen.

People suggested this

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

Which will not work on the BlackBerry, you will need to use this instead

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

But since it's only showing the exception, it will not give you the line where the error occured, you will have to add other information within the println .

On a real device you can get access to the StackTrace by doing this :

Go to the home screen and typing the back-door sequence LGLG. You then filter through the log and locate the exception entry. You can then copy and send the trace via email.

The best I could find on the RIM website is this document .

Ok, I can't take the credit for this but this thread seems to give the answer.

Change your catch block to catch Throwable instead of the specific Exception. This will keep the stack trace and add it to the event log.

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

Also, try running your application on the blackberry smartphone simulator rather than your real phone as blackberry indicate in their documentation that you can call the printStackTrace function.

finding NPE originator is easy in blackberry. eg

1.insert this code anywhere.

String temp = null; temp.length();

  1. start simulator in debug mode from eclipse.
  2. when you reach at this code eclipse automatically suspend the execution of code and temp.length(); this line will be highlighted as green.
  3. on debug window you can see suspended exception null pointer exception.

I don't know about blackberry, but generally, exceptions have a stacktrace, where line numbers are shown:

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

So obtain the stacktrace and see. The stacktrace is obtained either by

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

or by letting it bubble up to a place where it is printed to the standard output.

Also, each exception has getStackTrace() method, which returns each line of the stack-trace as a StackTraceElement

You should be able to look at the exception's stack trace.

You can print this to a console doing something like this:

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

Note: This should NEVER appear in production code!

Look at the stack trace when the program crashes. It will tell you where (and usually what line of code, if it's available) the exception originated as well as which object was null.

If you do not see a stack trace, surround everything in your main method with what OMG Unicorns suggests .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM