J2ME (Java) - null pointer exception in display class

I am currently working with a MIDlet (I use Visual MIDlet) in Netbeans, and a NullPointerException is thrown, but I don't know why.

Note. An exception does not occur when a program runs on the emulator only when the OK button is pressed.

Here is the error I get

TRACE: <at java.lang.NullPointerException: 0>, Exception caught in Display class java.lang.NullPointerException: 0 at javax.microedition.lcdui.Display$ChameleonTunnel.callScreenListener(), bci=46 at com.sun.midp.chameleon.layers.SoftButtonLayer.processCommand(), bci=74 at com.sun.midp.chameleon.layers.SoftButtonLayer.soft1(), bci=37 at com.sun.midp.chameleon.layers.SoftButtonLayer.keyInput(), bci=36 at com.sun.midp.chameleon.CWindow.keyInput(), bci=38 at javax.microedition.lcdui.Display$DisplayEventConsumerImpl.handleKeyEvent(), bci=17 at com.sun.midp.lcdui.DisplayEventListener.process(), bci=277 at com.sun.midp.events.EventQueue.run(), bci=179 at java.lang.Thread.run(Thread.java:619) 

I deleted all the code not related to the exception, so you can read it easier. Below is a simplified version if I have code that throws an exception above.

 package stMidlet; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class StoryMidlet extends MIDlet implements CommandListener { private boolean midletPaused = false; private Command commandOk1; private Form form1; private TextField textField1; public StoryMidlet() { commandOk1 = new Command("Ok", Command.OK, 1); textField1 = new TextField("Enter value: ", null, 120, TextField.ANY); form1 = new Form(null, new Item[]{textField1}); form1.addCommand(commandOk1); Display.getDisplay(this).setCurrent(form1); } /* There were some methods here pre-inserted by netbeans. */ /* I have not changed these, but I can post them if you need me too */ /* initialize() */ /* startMIDlet() */ /* resumeMidlet() */ /* switchDisplayable */ /* getDisplay() */ /* exitMidlet() */ /* startApp() */ /* pauseApp() */ /* destroyApp() */ public void commandAction(Command c, Displayable d) { if (c == commandOk1) { System.out.println("Test"); } } 

}

I try to solve this, at least for an hour, without exaggeration. The only thing I can think of is worth mentioning:

  • Netbeans displays a warning with the Display.getDisplay (this) ..... line indicating a leak in the constructor. I moved it to the initialize () method, which reassured the warning, but the exception still occurs.

Any help would be greatly appreciated.

Thanks Tom.

+6
java nullpointerexception multithreading java-me lcdui
source share
4 answers

EDIT: I may need to edit my answer because I think that what I said is irrelevant, but I will leave it out of the way so that it can help!

It has been a long time since I worked in J2ME, but, looking at some old code, I noticed that I had never done anything useful in the constructor. I am sure that your call to Display.getDisplay (this) throws a NullPtrException, because something else has not been initialized. In fact, I am sure that using this pointer in the constructor quite accurately causes this type of error.

Try using Display in the startApp () function, and if this code I'm referring to is correct, you should save a boolean that will be noted if your MIDlet is already initialized or not.

Here you can see some old code here:

http://code.google.com/p/jmingle/source/browse/trunk/src/org/oep/jmingle/JMingle.java#68

+3
source share

You may need to add

form1.setCommandListener (this);

+1
source share

I also noticed this ....

 public Welcome(String k, ChatApp c) { super(k); name = new TextField("Name", "", 140, TextField.ANY); exit = new Command("Exit", Command.EXIT, 0); enter = new Command("Enter", Command.OK, 0); midlet = c; this.append(name); this.addCommand(exit); this.addCommand(enter); } 

Disadvantage

 this.setCommandListener(this); 

and always got a null pointer exception ... This works well, but it seems like we forget it too often when our code gets too complicated ^^

+1
source share

You should do in order:

  • form1.setCommandListener
  • form1.addCommand ..
  • ....
  • after all: MIDlet.getDisplay.setCurrent(form1);

If you call "setCurrent" before adding commands and a listener, the form1 screen still appears, but when you click on a command, it rises above the error.

0
source share

All Articles