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

4

6

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.

Gunilla answered 22/1, 2011 at 19:11 Comment(2)
Debugging for only an hour? You have more in the tank. On a serious note, take a deep breath and walk away for a few hours. You'll definitely get it if someone on SO doesn't first.Shizukoshizuoka
Display.getDisplay(this) is said to leak because 'this' is escaping the constructor. Based on your constructor set up however, this is unlikely to cause the NullPointerEXception (as everything has already been initialized)Myungmyxedema
H
3

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

Hurleigh answered 24/1, 2011 at 4:49 Comment(0)
S
1

Maybe you need to add

form1.setCommandListener(this);

Seventh answered 24/1, 2011 at 15:33 Comment(0)
O
1

I also noticed it....

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);
    }

Lacked the

this.setCommandListener(this);

and always got the null-pointer exception... This works well but it seems that we forget it too often when our code gets too complex ^^

Octroi answered 24/6, 2011 at 4:59 Comment(0)
B
0

You must do in order:

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

If you invoke 'setCurrent' before adding the commands and listener, the form1 screen still appear but when you hit a command, it raises above error.

Bragg answered 26/6, 2012 at 17:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.