当前位置: 代码迷 >> J2ME >> 存储有关问题
  详细解决方案

存储有关问题

热度:5704   发布时间:2013-02-25 21:35:02.0
存储问题
java.lang.NullPointerException
at javax.microedition.lcdui.Displayable.addCommand(+11)
at RecordStoreExample.<init>(+88)
at java.lang.Class.runCustomCode(+0)
at com.sun.midp.midlet.MIDletState.createMIDlet(+34)
at com.sun.midp.midlet.Selector.run(+22)
程序如下:

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.*;
public class RecordStoreExample extends MIDlet implements CommandListener
{
private Display display;
private Alert alert;
private Command exit;
private Form form;
private Command start;
private RecordStore recordstore=null;
private RecordEnumeration recordnumeration=null;
public RecordStoreExample()
{
display=Display.getDisplay(this);
exit=new Command("退出",Command.EXIT,1);
exit=new Command("开始",Command.SCREEN,1);
form=new Form("Record store");
form.addCommand(exit);
form.addCommand(start);
form.setCommandListener(this); //监听
}
public void startApp()
{
display.setCurrent(form);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command command,Displayable d)
{
if(command==exit)
{ destroyApp(true);
notifyDestroyed();
}
else if(command==start)
{
try
{
recordstore=RecordStore.openRecordStore("myRecordStore",true);
}
catch(Exception error)
{
alert=new Alert("Error Creating",error.toString(),null,AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
try
{
recordstore.closeRecordStore();
}
catch(Exception error)
{
alert=new Alert("Error close",error.toString(),null,AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
if(RecordStore.listRecordStores()!=null)
{
try
{
RecordStore.deleteRecordStore("myRecordStore");
}
catch(Exception error)
{
alert=new Alert("Error removing",error.toString(),null,AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}
}
}
}
大家帮忙看看哦..感激不尽..


------解决方案--------------------------------------------------------
你也太粗心了,Command start没有初始化,看你的代码:
exit=new Command("退出",Command.EXIT,1); 
exit=new Command("开始",Command.SCREEN,1); 

应该是
exit=new Command("退出",Command.EXIT,1); 
start=new Command("开始",Command.SCREEN,1); 
  相关解决方案