专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Java Exception

异常-Caused by: java.lang.NullPointerException,通过MIDlet类调用Form的派生类FormOfSMS

发布时间:2010-06-05 12:36:28 文章来源:www.iduyao.cn 采编人员:星星草

我通过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();

    
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: