空指针异常怎么解决?
代码如下:
package list;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ListTest1 extends MIDlet implements CommandListener{
private List[] lists;
private Command nextCommand = new Command("Next",Command.SCREEN,1);
private Command exitCommand = new Command("Exit",Command.EXIT,1);
private String[] options={"option a","option b","option c"};
private Display display;
/** Constructor */
public ListTest1() {
lists=new List[3];
lists[0]=new List("Implicit-choice list",List.IMPLICIT,options,null);
lists[0].addCommand(nextCommand);
lists[0].addCommand(exitCommand);
lists[0].setCommandListener(this);
lists[1]=new List("Exclusive-choice list",List.IMPLICIT,options,null);
lists[1].addCommand(nextCommand);
lists[1].addCommand(exitCommand);
lists[1].setCommandListener(this);
lists[2]=new List("Multiple-choice list",List.IMPLICIT,options,null);
lists[2].addCommand(nextCommand);
lists[2].addCommand(exitCommand);
lists[2].setCommandListener(this);
}
/** Main method */
public void startApp() throws MIDletStateChangeException{
display.setCurrent(lists[0]);
}
/** Handle pausing the MIDlet */
public void pauseApp() {
}
/** Handle destroying the MIDlet */
public void destroyApp(boolean unconditional) {
lists=null;
nextCommand=null;
exitCommand=null;
display=null;
}
public void commandAction(Command c,Displayable d){
if(c==nextCommand){
List l=null;
for(int i=0;i<3;i++){
if(d==lists[i]) l=lists[(i+1)%3];
}
display.setCurrent(l);
}
else if(c==exitCommand){
destroyApp(true);
notifyDestroyed();
}
}
}
------解决方法--------------------------------------------------------
display是null。
在startApp第一行加
display=Display.getDisplay(this);