我的程序已经写好,而且编译通过,但是在save的时候不能将数据保存到数据库中
提示为:java.lang.NullPointerException
at Fourth.doSave(+221)
at Fourth.commandAction(+457)
at javax.microediton.ledui.Display$DisplayAccessor.commandAction(+152)
at com.sun.kvem.midp.lcdui.EnmlEventHandle$EventLoop.run(+459)
下面是我关于保存的代码,希望好心的大侠相助,小妹感激不尽!谢谢
private void doSave(){
RecordStore rs=null;
byte[] nameEmail=null;
boolean existingOrNot=false;
existingOrNot=existing("aPerson");
if(existingOrNot){
try{
rs=RecordStore.openRecordStore("aPerson",false);
}
catch(Exception e){
}
}
else{
try{
rs=RecordStore.openRecordStore("aPerson",true);
}
catch(Exception e){
}
}
Person aPerson=null;
try{
aPerson=new Person();
aPerson.write(nameField.getString(),
EMailField.getString(),
Integer.parseInt(ageField.getString())
);
byte[] data=aPerson.changeToByteArray();
int recordID=aPerson.getRecordID();
if(modificationID!=0)
recordID=modificationID;
System.out.println("record ID: "+recordID);
if(recordID!=-1){
rs.setRecord(recordID,data,0,data.length);
modificationID=0;
}
else{
recordID=rs.addRecord(data,0,data.length);
aPerson.setRecordID(recordID);
}
aPerson=null;
nameField.setLabel("消息");
nameField.setString("完成新增!");
}
catch(Exception e){
nameField.setLabel("注意");
nameField.setString("失败!");
}
try{
if(rs!=null)
rs.closeRecordStore();
//rs.deleteRecordStore("aPerson");
}
catch(Exception e){
}
}
class Person{
private int ID=-1;
private String name;
private String EMail;
private int age;
public void write(String name,String EMail,int age){
this.name=name;
this.EMail=EMail;
this.age=age;
}
public void setRecordID(int ID){
this.ID=ID;
}
public int getRecordID(){
return ID;
}
public byte[] changeToByteArray(){
byte[] data=null;
try{
ByteArrayOutputStream baos=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(baos);
dos.writeUTF(name);
dos.writeUTF(EMail);
dos.writeInt(age);
data=baos.toByteArray();
baos.close();
dos.close();
}
catch(Exception e){
}
return data;
}
public void changeFromByteArray(byte[] data){
try{
ByteArrayInputStream bais=new ByteArrayInputStream(data);
DataInputStream dis=new DataInputStream(bais);
name=dis.readUTF();
EMail=dis.readUTF();
age=dis.readInt();
bais.close();
dis.close();
}
catch(Exception e){
}
}
public String getName(){
return name;
}
public String getEMail(){
return EMail;
}
public int getage(){
return age;
}
}
class nameFilter implements RecordFilter{
public boolean matches(byte[] candidate){
DataInputStream person=new DataInputStream(new ByteArrayInputStream(candidate));
String name="";
String EMail="";
int age=0;
try{
name=person.readUTF();
EMail=person.readUTF();
age=person.readInt();
}
catch(Exception e){
}
if(name.equals(Fourth.criteria))
return true;
else
return false;
}
}