4

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?

navand
  • 1,379
  • 1
  • 16
  • 20
  • See similar issue at http://stackoverflow.com/questions/2411487/nullpointerexception-in-java-with-no-stacktrace – Martin Jan 01 '14 at 06:42

7 Answers7

4

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
  • 1,735
  • 11
  • 17
2

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.
  • 3,410
  • 1
  • 21
  • 32
  • +1. Why there are not stacktraces on BB? Are they just not supported by BB Jvm? – pajton Apr 13 '10 at 19:43
  • @pajton I wish I knew the answer to this question :P – Michael B. Apr 13 '10 at 19:49
  • 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. – hookenz Apr 14 '10 at 05:14
  • @Matt H if you know how to run BlackBerry Code on a PC, tell me how. – Michael B. Apr 14 '10 at 11:38
  • Have you tried printing stack trace on the blackberry smartphone simulator? their docs seem to indicate printStackTrace works – hookenz Apr 15 '10 at 03:26
2

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.

hookenz
  • 36,432
  • 45
  • 177
  • 286
0

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
  • 14,900
  • 6
  • 36
  • 74
-1

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
  • 588,226
  • 146
  • 1,060
  • 1,140
-1

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!

Powerlord
  • 87,612
  • 17
  • 125
  • 175
  • 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 Apr 13 '10 at 17:36
  • This is not going to work, you need to use `System.out.println(e.getMessage());` – Michael B. Apr 13 '10 at 18:42
  • 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 Apr 13 '10 at 19:42
-1

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.

Community
  • 1
  • 1
Ben S
  • 68,394
  • 30
  • 171
  • 212