2


I'm stuck trying to figure out how to create a back command to the previous screen.
The page I'm trying to return to does not have a form but a List but when I set the
'back' command listener to the list it just seems to throw a null pointer exception.

Here is my main class

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

/**
 * 
 */
public class CalFrontEnd extends MIDlet implements CommandListener 
{
    private Display display;
    protected List list = new List("Please Select a 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;


    /**
     * 
     */
    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
     */
    public void startApp() 
    {
        display.setCurrent(list);
    }


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


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


    /**
     * 
     * @param command
     * @param displayable 
     */
    public void commandAction(Command command, Displayable displayable) 
    {
        if (displayable == list) {
            if (command == List.SELECT_COMMAND) {
                switch (list.getSelectedIndex()) {
                    case 0: // select Date
                        SelectDate myDate = new SelectDate(display);
                        myDate.BuildCalendar();
                        break;
                    case 1: //add Events
                        AddEvents myAEvents = new AddEvents(display);
                        myAEvents.BuildAddEvents();
                        break;
                    case 2: //Remove Events
                        RemoveEvents myREvents = new RemoveEvents(display);
                        myREvents.BuildRemoveEvents();
                        break;
                    case 3: //Browse Events
                        BrowseEvents myBEvents = new BrowseEvents(display);
                        myBEvents.BuildBrowseEvents();
                        break;
                }
            } else if (command == exit) {
                destroyApp(false);
                notifyDestroyed();
            }
        }         
    } 
}

And this is the class which I'm trying to use the back button on

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


/**
 * 
 */
public class SelectDate extends CalFrontEnd implements CommandListener 
{
    private DateField calendar;
    private Form form = new Form("Please Select a Date");
    private Command select = new Command("Select", Command.SCREEN, 1);
    private Command back = new Command("Back", Command.BACK, 2);
    private Command save = new Command("Save,", Command.SCREEN, 2);
    private Display display;


    /**
     * 
     */
    public SelectDate(Display display)
    {
        this.display = display;
    }


    /**
     * 
     */
    public void BuildCalendar()
    {
        calendar = new DateField("Date In :", DateField.DATE, TimeZone.getTimeZone("GMT"));
        form.append(calendar);
        form.addCommand(back);
        form.setCommandListener(this);
        display.setCurrent(form);
    }  


    /**
     * 
     * @param command
     * @param displayable 
     */
    public void commandAction(Command command, Display display)
    {
        if (command == back)
        {
            display.setCurrent(list);
        }
    }
}
gnat
  • 6,213
  • 108
  • 53
  • 73
Skeng
  • 147
  • 3
  • 11

3 Answers3

0

Inappropriate use of inheritance has brought you into trouble here. Look, there is a list field in SelectDate class but it is not visible in code, because it is inherited from superclass (extends CalFrontEnd and protected List list are where all your trouble really begins).

When you create an instance of SelectDate (new SelectDate(display)) this field is initialized with null - and you never change it after that. It's hard for you to notice that because the very declaration of list is buried in other file in superclass. And, which makes things even harder, compiler can't help you here because the field is visible to it and it believes things are OK.

  • You know, this is only a beginning of your troubles related to overuse of inheritance. Further down the road, more headaches are waiting to happen. Think for example of what would happen if you accidentally remove or rename commandAction in SelectDate? Compiler will think it's all-right - just because superclass has this method, too. You'll only notice that things went wrong in some misterious way when you run the code and find out that commands at select date screen stop responding or began doing weird things. Actually it would be safer to hide CommandListener for both classes just to avoid this kind mistakes but that was discussed in another question.

I strongly recommend to wipe out extends CalFrontEnd from SelectDate. That will help compiler help you to find various logical issues in your code.

As for list to show by Back command, you can for example pass it as additional constructor parameter, like as below:

public class SelectDate implements CommandListener // drop "extends CalFrontEnd"
{
    // ...
    private Display display;
    private List list; // add the field for the list


    public SelectDate(Display display, List list) // add list as parameter
    {
        this.display = display;
        this.list = list; // initialize field
    }
    // ... commandAction code will get the right "list" now
}
Community
  • 1
  • 1
gnat
  • 6,213
  • 108
  • 53
  • 73
0

There are a number of problems regarding your code.

One gnat has already mentioned (Remove extends CalFrontEnd in SelectData class).

Secondly you are not calling select command in commandAction of your code (command you are calling is List.SELECT_COMMAND which is not select). So change if (command == List.SELECT_COMMAND) to if (command == select).

Thirdly documentation of commandAction in CommandListener declares its second parameter to be Displayable while you are declaring it with Display.

Ameer Moaaviah
  • 1,530
  • 12
  • 27
-1

the error is that there's no variable called list, the solution however is to simply change the code under your back button from

display.setCurrent(list)

to

display.setCurrent(CalFrontEnd.list)