title: J2ME (Java) - Null Pointer Exception caught in Display Class
body: <p>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. </p>
  
  <p><strong>Note:</strong> The exception is not thrown when the program runs on the emulator, only when the OK Command button is pressed.</p>
  
  <p>Here is the error I get</p>
  
  <pre><code>TRACE: &lt;at java.lang.NullPointerException:   0&gt;, 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)
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>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");
          }
      }
  </code></pre>
  
  <p>}</p>
  
  <p>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:</p>
  
  <ul>
  <li>Netbeans showed a warning with the line <strong>Display.getDisplay(this).....</strong> saying there was a leak in the constructor. I moved it into the initialize() method which sedated the warning, but the exception still occurs.</li>
  </ul>
  
  <p>Any help will be greatly appreciated.</p>
  
  <p>Thanks, Tom.</p>