Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

J2ME (Java) - Null Pointer Exception caught in Display Class

I'm currently working with MIDlets (I am using a Visual MIDlet) in Netbeans, and a NullPointerException is being thrown but I do not know why.

Note: The exception is not thrown when the program runs on the emulator, only when the OK Command 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 have stripped out all of the code unrelated to the exception, so that you can read it easier. Below is a simplified version if the code I have, which throws the above exception.

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 have been trying to solve this for at least an hour, with no prevail. The only thing I can think of worth mentioning is:

  • Netbeans showed a warning with the line Display.getDisplay(this)..... saying there was a leak in the constructor. I moved it into the initialize() method which sedated the warning, but the exception still occurs.

Any help will be greatly appreciated.

Thanks, Tom.

like image 401
Tom Avatar asked Jan 22 '11 19:01

Tom


1 Answers

EDIT: I might need to redact my answer because I think what I said is not relevant, but I'll leave it up on the off-chance that it could help!

It's been a long time since I have worked in J2ME, but having looked at some old code I noticed I never did anything that useful in the constructor. I'm betting your call to Display.getDisplay(this) is causing a NullPtrException because something hasn't been initialized yet. In fact, I'm pretty sure using the this pointer in a constructor is fairly certain to cause this type of error.

Try dealing with Display in the startApp() function, and if this code I'm referring to is correct, you should keep a boolean that marks if your MIDlet has been initialized yet or not.

You can look at some old code of mine here for reference:

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

like image 96
OEP Avatar answered Sep 28 '22 00:09

OEP