读书人

错误-Caused by: java.lang.NullPoint

发布时间: 2012-12-24 10:43:14 作者: rapoo

异常-Caused by: java.lang.NullPointerException,通过MIDlet类调用Form的派
点击右边红色标题查看本文完整版:异常-Caused by: java.lang.NullPointerException,通过MIDlet类调用Form的派生类FormOfSMS

我通过MIDlet类调用Form的派生类FormOfSMS,初始化时this.addCommand(cmdOk);this.addCommand(cmdCancel);总是报异常Caused by: java.lang.NullPointerException
at java.util.Hashtable.get(Unknown Source)
at javax.microedition.lcdui.Display.getDisplay(Unknown Source)
at emulator.Emulator.getCurrentDisplay(Unknown Source)
at javax.microedition.lcdui.Displayable.addCommand(Unknown Source)
at FormOfSMS.initCommand(FormOfSMS.java:42)
at FormOfSMS. <init>(FormOfSMS.java:34)
at FriendsMIDlet. <clinit>(FriendsMIDlet.java:18)
... 3 more
说是空指针,可是我断点跟时候command是有内容的,小弟不解,望指点代码如下

import javax.microedition.lcdui.*;


public class FormOfSMS extends Form implements CommandListener
{
private TextField tf1 = null;

static Command cmdOk = null; //确定按钮
static Command cmdCancel = null; //取消按钮

public FormOfSMS()
{


super("发送短信");

tf1=new TextField("短信内容", null, 100,TextField.ANY);

this.append(tf1);



initCommand();
}

void initCommand()
{
cmdOk = new Command("确定", Command.OK, 1);
cmdCancel = new Command("取消", Command.OK, 1);

this.addCommand(cmdOk);
this.addCommand(cmdCancel);

this.setCommandListener(this);


FriendsMIDlet.disp.setCurrent(this);
}


public void commandAction(Command command, Displayable displayable)
{
? if (command == this.cmdOk)
? {
//? SMS();
? }
? else if(command == this.cmdCancel)
? {
//? Display.getDisplay(m).setCurrent(m.disp);
? }
}
}

------解决方法--------------------
disp是null,原因是因为MIDlet中代码的书写顺序,因为类中的属性初始化先于构造方法!

public static FormOfSMS dispSMS=null;

public static Display? disp=null;
//private splashCanvas sc ;

public FriendsMIDlet()
{
instance=this;
disp=Display.getDisplay(instance);
dispSMS=new FormOfSMS();

    

读书人网 >编程

热点推荐