Search code examples
java-menullpointerexceptionmidplcdui

Setting displayable objects in multiple classes (J2ME)


I'm trying to make my code neater by using multiple classes for my applications
form options. Currently I keep getting null pointer exceptions when trying to setCurrent.

Here is my main class the error starts in my command listener when I call the other class.

import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

public class CalFrontEnd extends MIDlet implements CommandListener {

  private Display display;
  private List list = new List("Please Select a Option", List.IMPLICIT);
  private List Blist = new List("Please Select a Browsing Option", List.IMPLICIT);
  private Command select = new Command("Select", Command.SCREEN, 1);
  private Command exit = new Command("Exit", Command.EXIT, 2);
  private Command save = new Command("Save,", Command.SCREEN, 2);
  private DateField calendar;
  Alert alert;

  //
  //
  //
  public CalFrontEnd() {
    display = Display.getDisplay(this);   

    list.append("Select Date", null);
    list.append("Add Events", null);
    list.append("Remove Events", null);
    list.append("Browse Events", null);
    list.addCommand(select);
    list.addCommand(exit);
    list.setCommandListener(this);

  }


  //
  //Start Application Method
  //
  public void startApp() {
    display.setCurrent(list);
  }


  //
  //Pause Application Method
  //
  public void pauseApp() {
  }


  //
  //Destroy Application Method
  //
  public void destroyApp(boolean unconditional) {
  }


  //
  //Method creates form which contains calendar
  //
  /*public void selectDate()
  {
      calendar = new DateField("Date In :", DateField.DATE, TimeZone.getTimeZone("GMT"));
      Form cform = new Form("Calendar");
      cform.append(calendar);
      cform.addCommand(exit);
      display.setCurrent(cform);
  }*/


  //
  //Method creates form which contains adding events
  //
  public void AddEvents()
  {
      TextBox aeText = new TextBox("Add Event","", 256, 0);
      display.setCurrent(aeText);
  }


  //
  //Method creates form which contains removing events
  //
  public void RemoveEvents()
  {

      Form reform = new Form("Remove Event");
      reform.append(calendar);
      display.setCurrent(reform);
  }


  //
  //Method creates form which contains removing events
  //
  public void BrowseEvents()
  {
      Blist.append("Monthly", null);
      Blist.append("Daily", null);
      Blist.addCommand(select);
      Blist.addCommand(exit);
      Blist.setCommandListener(this);
      display.setCurrent(Blist);
  }


  //
  //but it's better practice to make each form a different class extending CommandListener and it's own commandAction. And leave the display public static in MIDlet class
  //...
  public void commandAction(Command command, Displayable displayable) {
      if (displayable == list) {
          if (command == List.SELECT_COMMAND) {
              switch (list.getSelectedIndex()) {
                  case 0: // select date
                      SelectDate.BuildCalendar(); //Error Here
                      break;
                  case 1: //add events
                      AddEvents();
                      break;
              }
          } else if (command == exit) {
             destroyApp(false);
             notifyDestroyed();
          }
      }         
  }
}

And here is the class that is being called.

public class SelectDate
{

    private static DateField calendar;
    private static Form form = new Form("derb");
    private static Command select = new Command("Select", Command.SCREEN, 1);
    private static Command exit = new Command("Exit", Command.EXIT, 2);
    private static Command save = new Command("Save,", Command.SCREEN, 2);
    private static Display display;


    public static void BuildCalendar()
    {
        calendar = new DateField("Date In :", DateField.DATE, TimeZone.getTimeZone("GMT"));
        form.append(calendar);
        form.addCommand(exit);
        display.setCurrent(form);
    }  
}

Solution

  • The NullPointerException happens because display in SelectDate class is null.

    To fix that, you can for example drop it from there and instead, add to method parameters:

    // ...
    public static void BuildCalendar(Display display) // added parameter
    

    Then, when you invoke above method from CalFrontEnd, pass the instance of display there:

                          // ...
                          SelectDate.BuildCalendar(display); //Error will go away