Question

I've just created a fairly simple Java ME app for the Series 40 platform. The app runs perfectly in the emulator, but when I package it to my own Series 40 phone, the app fails with a NullPointerException. Almost all of my code handles arrays, and so it is very hard to tell where the error has occured, as the phone's error report does not include any details.

So how can I debug the app on the phone? (And if anyone can think of any reason why an app would fail on the phone, but not the emulator (I'm really stumped on this) please share.)

Était-ce utile?

La solution

One of the many rules of thumbs with JavaME is "Never trust emulators - always test on multiple real devices".

NullPointerExceptions are the most tricky ones to locate, but not impossible. I've done it many times by simply outputting an error message on the device.

String error = null;
Image myImage = null;

try {
    myImage = Image.createImage("someImage.png");
} catch (Exception e) {
    error = "Failed to load someImage.png: " + e.toString();
}

Then in your main loop (for Canvas / GameCanvas stuff), do

if (error!=null) g.drawString(error,0,0,0);

Then start inserting a lot of try/catch blocks in your code, whereever you are creating objects.

If the above myImage didn't load, then you'll get a NullPointerException when trying to

g.drawImage(myImage,0,0,0);

Autres conseils

To identify the block in which the exception is being thrown, you could wrap likely areas in try/catch. Catch the NullPointerException and throw a more obscure exception that will help you narrow it down. So one could throw ArithmeticException etc.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top