views:

427

answers:

8

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?

+1  A: 

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

Bozho
There are no StackTrace on the BlackBerry, this will never be shown.
Michael B.
+1  A: 

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!

R. Bemrose
The point is, the stacktraces are not just printed on BlackBerry. I am also developing for BlackBerry and would like to know how to get them printed. What you get instead is just "No stack trace" printed.
pajton
This is not going to work, you need to use `System.out.println(e.getMessage());`
Michael B.
Sure, but then I just get the message and not a stacktrace. Do you know how to get the stacktrace printed? Ok, I see you posted an answer on that, thnx.
pajton
A: 

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.

Ben S
There are no StackTrace on the BlackBerry, this will never be shown.
Michael B.
+1  A: 

You can use a stack trace to follow back to the line at which the error occurred. If you have more than one value that might throw a null pointer exception, you can put a break point at that line an examine the variables when you run the program in debug mode. Lastly, you can print each value in seperate print statements. This should throw a null pointer exception when you are trying to print an item that is null, thus helping you pin point the variable that is throwing the null pointer exception.

npinti
There are no StackTrace on the BlackBerry, this will never be shown.
Michael B.
I don't recall that you can write applications on your BlackBerry either... so I assumed that an IDE is being used.
npinti
@npinti Yes eclipse is used to program BlackBerry application.
Michael B.
I have never used eclipse for mobile applications, but I presume it has some sort of stack trace functionality...
npinti
@npinti The problem is not eclipse, but the emulator. See my answer.
Michael B.
+3  A: 

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.

Michael B.
+1. Why there are not stacktraces on BB? Are they just not supported by BB Jvm?
pajton
@pajton I wish I knew the answer to this question :P
Michael B.
Well it's java right? so you can run your app on the PC as well. Is the bug repeatable? That might give you some clues hopefully since michael is saying there is no stacktrace. But PC has it.
Matt H
@Matt H if you know how to run BlackBerry Code on a PC, tell me how.
Michael B.
Have you tried printing stack trace on the blackberry smartphone simulator? their docs seem to indicate printStackTrace works
Matt H
A: 

You can view the stacktrace if you catch Throwable instead of catching Exception or any subclass of Exception. e.g.

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.  
}
Jonathan
A: 

finding NPE originator is easy in blackberry. e.g.

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.
Vivart
A: 

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/m-p/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.

Matt H