×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Regular Contributor
    Join Date
    Mar 2010
    Posts
    136

    reading data from recordstore

    I am testing just a simple Recordstore program.
    In my program, Whenever i choose open command, It will write two string ( excellent and nice ) to the Recordstore "comment".
    And when I choose Delete command, i want to delete all records.
    But in my program, After choosing delete command, if I choose the open command, it does not work.
    It's error message is
    javax.microedition.rms.InvalidRecordIDException: error finding record
    What is the problem?

    Code:
    package treadstringrecord;
    
    import javax.microedition.lcdui.Alert;
    import javax.microedition.lcdui.AlertType;
    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.midlet.MIDletStateChangeException;
    import javax.microedition.rms.RecordStore;
    import javax.microedition.rms.RecordStoreException;
    import javax.microedition.rms.RecordStoreNotOpenException;
    
    public class Mainformcommandhandler implements CommandListener {
    	//Display display=Display.getDisplay(TReadStringRecordMIDlet.treadstringrecordmidlet);
    	Display display;
    	RecordStore rs;
    	Alert alert;
    
    	public Mainformcommandhandler() {
    		// TODO Auto-generated constructor stub
    	}
    
    	public void commandAction(Command c, Displayable d) {
    		MainForm mainform=(MainForm)d;
    		if(c==mainform.cmdExit)
    		{
    			try {
    				TReadStringRecordMIDlet.treadstringrecordmidlet.destroyApp(true);
    			} catch (MIDletStateChangeException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();/////////////////////////////////see how that is written in the book
    			}
    		}
    		
    		
    		if(c==mainform.cmdOpen)
    		{
    			try {
    				rs=RecordStore.openRecordStore("comment", true);
    			} catch (RecordStoreException e) {
    				// TODO Auto-generated catch block
    				//e.printStackTrace();
    				alert=new Alert("Cannot open recordstore",e.toString(),null,AlertType.ERROR);
    				display=Display.getDisplay(TReadStringRecordMIDlet.treadstringrecordmidlet);
    				display.setCurrent(alert);
    				
    				
    			}
    			
    			String comment1="Excellent";
    			String comment2="very nice";
    			//byte[] readbyte=null;
    			byte[] commentbyte =comment1.getBytes();
    			byte[] commentbyte2=comment2.getBytes();
    			try {
    				rs.addRecord(commentbyte, 0, commentbyte.length);
    				rs.addRecord(commentbyte2, 0, commentbyte2.length);
    			} catch (RecordStoreException e) {
    				// TODO Auto-generated catch block
    				alert=new Alert("Cannot write recordstore",e.toString(),null,AlertType.ERROR);
    				display=Display.getDisplay(TReadStringRecordMIDlet.treadstringrecordmidlet);
    				display.setCurrent(alert);
    			}
    			
    			try {
    				StringBuffer buffer=new StringBuffer();
    				for(int i=1;i<=rs.getNumRecords();i++)
    				{					
    					String readrecord=new String(rs.getRecord(i));
    					buffer.append(readrecord);
    				}
    				alert=new Alert("Reading recordstore",buffer.toString(),null,AlertType.INFO);
    				display=Display.getDisplay(TReadStringRecordMIDlet.treadstringrecordmidlet);
    				display.setCurrent(alert);
    				alert.setTimeout(Alert.FOREVER);
    			} catch (RecordStoreNotOpenException e) {
    				
    				alert=new Alert("There is no recordstore",e.toString(),null,AlertType.ERROR);
    				display=Display.getDisplay(TReadStringRecordMIDlet.treadstringrecordmidlet);
    				display.setCurrent(alert);
    			} catch (RecordStoreException e) {
    				
    				alert=new Alert("error in reading recordstore",e.toString(),null,AlertType.ERROR);
    				display=Display.getDisplay(TReadStringRecordMIDlet.treadstringrecordmidlet);
    				display.setCurrent(alert);
    				alert.setTimeout(Alert.FOREVER);
    			}
    			
    			try {
    				rs.closeRecordStore();
    			} catch (RecordStoreException e) {
    				
    				e.printStackTrace();
    			}			
    				
    		}//if (c==mainform.cmdOpen)
    		
    		
    		if(c==mainform.cmdDelete)
    		{
    			try {
    				rs=RecordStore.openRecordStore("comment", false);
    			
    			
    			
    			try {
    				int i=rs.getNumRecords();
    				while(i!=0)
    				{
    					rs.deleteRecord(i);
    					i--;
    				}
    			} catch (RecordStoreNotOpenException e) {
    				
    				e.printStackTrace();
    			} catch (RecordStoreException e) {
    				
    				e.printStackTrace();
    			}
    			} catch (RecordStoreException e) {				
    				alert=new Alert("Cannot open recordstore",e.toString(),null,AlertType.ERROR);
    				display=Display.getDisplay(TReadStringRecordMIDlet.treadstringrecordmidlet);
    				display.setCurrent(alert);				
    			}
    		}//if(c==mainform.cmdDelete)
    		
    	}
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    Noida
    Posts
    941

    Re: reading data from recordstore

    In RMS Record IDs are not re-used, so after deleting record 1and 2, when you do OPEN command again it adds record 3 and 4.
    Now when you try to read Record ID 1 and 2 it fails and give exception here.
    Problem code:
    for(int i=1;i<=rs.getNumRecords();i++)
    {
    String readrecord=new String(rs.getRecord(i));
    buffer.append(readrecord);
    }


    Fix will be for(int i=getNumRecords()-1;i<=rs.getNumRecords();i++)
    Thanks,
    Ekta

  3. #3
    Regular Contributor
    Join Date
    Mar 2010
    Posts
    136

    Re: reading data from recordstore

    I have changed the code. But it still does not work. And the error message is still the same.

    Quote Originally Posted by ektasrv View Post
    In RMS Record IDs are not re-used, so after deleting record 1and 2, when you do OPEN command again it adds record 3 and 4.
    Now when you try to read Record ID 1 and 2 it fails and give exception here.
    Problem code:
    for(int i=1;i<=rs.getNumRecords();i++)
    {
    String readrecord=new String(rs.getRecord(i));
    buffer.append(readrecord);
    }


    Fix will be for(int i=getNumRecords()-1;i<=rs.getNumRecords();i++)

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    Noida
    Posts
    941

    Re: reading data from recordstore

    OK then go for another approach, if you want to delete all records, delete the record Store also after closing your original code will work.
    Thanks,
    Ekta

  5. #5
    Regular Contributor
    Join Date
    Mar 2010
    Posts
    136

    Re: reading data from recordstore

    That works now.
    Thanks ektasrv.

    Quote Originally Posted by ektasrv View Post
    In RMS Record IDs are not re-used, so after deleting record 1and 2, when you do OPEN command again it adds record 3 and 4.
    Now when you try to read Record ID 1 and 2 it fails and give exception here.
    Problem code:
    for(int i=1;i<=rs.getNumRecords();i++)
    {
    String readrecord=new String(rs.getRecord(i));
    buffer.append(readrecord);
    }


    Fix will be for(int i=getNumRecords()-1;i<=rs.getNumRecords();i++)

Similar Threads

  1. writing and reading data to data cable's port using socket
    By haroonob in forum Mobile Java Networking & Messaging & Security
    Replies: 1
    Last Post: 2009-08-27, 07:42
  2. Problem with persisting data into the RecordStore
    By ionutdavid_eu in forum Mobile Java General
    Replies: 1
    Last Post: 2007-12-19, 12:34
  3. Reading data from Data stream
    By hwliew in forum Mobile Java Networking & Messaging & Security
    Replies: 1
    Last Post: 2007-01-08, 18:24
  4. How to transfer RecordStore data to PC using USB cable?
    By rohitverma81 in forum Mobile Java Networking & Messaging & Security
    Replies: 0
    Last Post: 2006-11-14, 07:25
  5. how to put data at a recordstore
    By naneziris in forum Mobile Java General
    Replies: 2
    Last Post: 2005-09-03, 18:42

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×