RMS的使用出现错误。

CaptainGan 2008-11-24 01:48:53
下面的是MIDlet的调用类的部分的代码,我不知道哪里错了。
public void commandAction(Command cmd,Displayable display)
{
if(cmd==okCommand)
{
if(currentForm.equals("Menu Form List"))
{
javax.microedition.lcdui.List down= (javax.microedition.lcdui.List)display1.getCurrent();
switch(down.getSelectedIndex())
{
case 0:showMessageForm();break;
case 1:showPhoneBookForm();break;
}
}
else if(currentForm.equals("Message"))
{
javax.microedition.lcdui.List down= (javax.microedition.lcdui.List)display1.getCurrent();
switch(down.getSelectedIndex())
{
case 0:showInput();break;
case 1:break;
case 2:break;
}
}
else if(currentForm.equals("Phone Book"))//1号错误: {
javax.microedition.lcdui.List down= (javax.microedition.lcdui.List)display1.getCurrent();
switch(down.getSelectedIndex())
{
case 0:showAddForm();break;
case 1:
int num=rsc. numOfRecord();
for(int i=0;i<num;i++)
{
System.out.println(rsc.getRecordStore(i));
}
break;
case 2:break;
}
}
}

else if(cmd==addCommand)//2号错误:
{
rsc= new RecordStoreCom();
rsc.openStore("PhoneBookStore");
String name=nameTextField.getString();
String mobile=mobileTextField.getString();
String perPhone=perTextField.getString();
String email=emailTextField.getString();
// String date=Calendar.YEAR+":"+Calendar.MONTH+":"+Calendar.DAY_OF_MONTH+":"+Calendar.DATE;
String contactor=name+"/"+mobile+"/"+perPhone+"/"+email;
errorMessageAlert.setString("Input some message into textforms.");
display1.setCurrent(errorMessageAlert);
rsc.addRecordStore(contactor);
System.out.println(rsc.getRecordStore(1));

}
}

下面的是RecordStoreCom.java的类。其中是RMS的方法。

public class RecordStoreCom
{
String name;
String message;
String toPhone;
String fromPhone;
String date;
RecordStore recordStore=null;
//获得存储记录的数量
public int numOfRecord()
{
int numRecords=0;
try
{
numRecords=recordStore.getNumRecords();
}
catch(RecordStoreException e)
{
System.out.println("Error in getting the number of recordStore---"+e);
}
return numRecords;
}
//打开存储记录.
public RecordStore openStore(String fileName)
{
try
{
recordStore=RecordStore.openRecordStore(fileName,true);
}
catch(RecordStoreException rse)
{
rse.printStackTrace();
}
return recordStore;
}
//修改存储中的信息并保存。
。。。。。。。。。。。。。
//获得存储的信息的方法
public String getRecordStore(int recordId)
{
Information Info= new Information();
int num=0;
String message=null;
try
{
num=recordStore.getNumRecords();
}
catch(RecordStoreException e)
{
System.out.println("Get recordStore Exception:"+e);
}
byte[] data=null;
if(num>0)
{
try
{
data=recordStore.getRecord(recordId);
Info.decode(data);
message+="Name:"+Info.name;
message+="\n Mobile Phone:"+Info.mobile;
message+="\n Permanent Phone"+Info.perPhone;
message+="\n Email:"+Info.email;
}
catch (InvalidRecordIDException e) { }
catch(RecordStoreException e)
{
System.out.println("GetRecordStore Exception:"+e);
}
}
return message;
}
//对信息进行格式化.
public class Information
{
String name;
String mobile;
String perPhone;
String email;
public void decode(byte[] data)
{
String mess=new String(data);
int pos1=mess.indexOf('/');
if (pos1>=0) {
name = mess.substring(0,pos1);
int pos2 = mess.indexOf('/',pos1+1);
if (pos2>pos1) {
mobile= mess.substring(pos1+1,pos2);
int pos3 = mess.indexOf('/',pos2+1);
if (pos3>pos2) {
perPhone = mess.substring(pos2+1,pos3);
email = mess.substring(pos3+1,mess.length());
}
}
}
}
}

//删除存储中的信息.
。。。。。。。。。。
//添加存储记录的信息
public boolean addRecordStore(String mesContent)
{

byte[] data=mesContent.getBytes();
int id;
int recId=-1;
/*
try
{
id=recordStore.getNextRecordID();
}
catch(RecordStoreException e)
{
System.out.println("RecordStoreException : The RecordStore RecordID is "+recId);
}
*/
try
{
recordStore.addRecord(data,0,data.length);
}
catch(RecordStoreException e)
{
System.out.println("RecordStoreException : The RecordStore RecordID is "+recId);
}
return true;
}
}

将上面的代码进行调用会出现下面的错误。
1号错误:java.lang.NullPointerException
at RecordStoreCom.numOfRecord(+6)
at ShortMessage.commandAction(+210)
at javax.microedition.lcdui.Display$DisplayAccessor.commandAction(+282)
at javax.microedition.lcdui.Display$DisplayManagerImpl.commandAction(+10)
at com.sun.midp.lcdui.DefaultEventHandler.commandEvent(+68)
at com.sun.midp.lcdui.AutomatedEventHandler.commandEvent(+47)
at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.handleVmEvent(+186)
at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(+57)
2号错误:
java.lang.NullPointerException
at ShortMessage.commandAction(+666)
at javax.microedition.lcdui.Display$DisplayAccessor.commandAction(+282)
at javax.microedition.lcdui.Display$DisplayManagerImpl.commandAction(+10)
at com.sun.midp.lcdui.DefaultEventHandler.commandEvent(+68)
at com.sun.midp.lcdui.AutomatedEventHandler.commandEvent(+47)
at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.handleVmEvent(+186)
at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(+57)
...全文
176 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
haizj0216 2008-11-25
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 CaptainGan 的回复:]
上面的问题都不是主要的,即使改了还是出现上面的错误。
[/Quote]
是的,因为时间问题只是简单的看了下,如果是上面的问题会提示无效的ID异常。可见在此之前已经报出异常,楼主可以看看是不是其他地方出现问题。
lixiurui 2008-11-24
  • 打赏
  • 举报
回复
at RecordStoreCom.numOfRecord(+6)

public int numOfRecord()
{
int numRecords=0;
try
{
numRecords=recordStore.getNumRecords();
}
catch(RecordStoreException e)
{
System.out.println("Error in getting the number of recordStore---"+e);
}
return numRecords;
}
你确定case 1的时候rsc是指向一个对象的?
switch(down.getSelectedIndex())
{
case 0:showAddForm();break;
case 1:
int num=rsc. numOfRecord();
for(int i=0;i<num;i++)
{
System.out.println(rsc.getRecordStore(i));
}
break;
case 2:break;
}
CaptainGan 2008-11-24
  • 打赏
  • 举报
回复
上面的问题都不是主要的,即使改了还是出现上面的错误。
kf156 2008-11-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 haizj0216 的回复:]
大致看了下,调用getRecordStore()方法,参数从1开始。RMS记录的ID是从1开始的。
[/Quote]

嗯,确实,我疏忽了,这确实也是个问题。
haizj0216 2008-11-24
  • 打赏
  • 举报
回复
大致看了下,调用getRecordStore()方法,参数从1开始。RMS记录的ID是从1开始的。
kf156 2008-11-24
  • 打赏
  • 举报
回复
代码乱了点,看得有点晕
不过看你这代码RMS类的实例化以及打开(创建)在2号错误那
在执行1号错误发生的命令时,你是否已经执行过2号错误位置的命令了呢?
如果没有那报空指针是肯定的了

13,100

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧