编程论坛's Archiver

Phoebe 发表于 2006-6-28 11:10

请教一个关于向j2me数据库内写入数据的问题

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

LVTIAN 发表于 2006-6-28 13:36

空指针异常,自己再查一下

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.