×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    5

    Unhappy Record store does not work on mobile phone

    Hello all,

    I am developing an application for mobile phone with J2ME(CDLC, MIDP 2.0 ) for Nokia S60 using netbeans 6.5 IDE. I am recording a data using record store and then reading it. it is working fine in emulator. However, I install it on my mobile phone it saves data but unfortunately when I close application and then open it data does not exist anymore.
    any idea? Do I need to do any settings in my mobile phone or netbeans? my phone is Nokia e71.
    here is my code:
    public boolean save() {
    RecordStore rs = null;
    try {
    rs = RecordStore.openRecordStore("myNotes", true);


    } catch (RecordStoreException rse) {
    System.err.println(rse.toString());
    return false;
    }
    byte[] elementToBeSaved = this.toByte();

    try {
    rs.addRecord(elementToBeSaved, 0, elementToBeSaved.length);


    } catch (RecordStoreException ex) {
    ex.printStackTrace();
    return false;
    }
    return true;
    }

    elementToBeSaved is a Vector which then convert it to byte.

    here I read from store:
    public static Vector getMynotes() {
    Vector records = new Vector();
    Vector result = new Vector();
    RecordStore rs = null;
    try {
    rs = RecordStore.openRecordStore("myNotes", true);


    } catch (RecordStoreException rse) {
    System.err.println(rse.toString());
    return null;
    }
    try {
    for (int i = 1; i <= rs.getNumRecords(); i++) {
    // Get data into the byte array
    byte[] recordAsByte = rs.getRecord(i);
    records.addElement(new String(recordAsByte));
    }

    for (int i = 0 ; i<records.size() ; i++){

    }

    } catch (Exception e) {
    e.printStackTrace();
    }

    for (int i = 0; i < records.size(); i++) {
    String record = (String) records.elementAt(i);
    result.addElement(getMynotes(record));
    }
    return result;

    }



    Thanks

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Record store does not work on mobile phone

    From the code you post, you are not closing the record store. You must close the record store, or data will be lost. You need to ensure that record stores are closed in all situations, even if exceptions are thrown.

    For example:

    PHP Code:
    public static Vector getMynotes() throws RecordStoreException {
        
    Vector result = new Vector();
        
    RecordStore rs openRecordStore("myNotes"true);
        try {
            
    int num rs.getNumRecords();
            for (
    int i 1<= numi++) {
                
    byte[] data rs.getRecord(i);
                
    result.addElement(getMyNoted(data));
            }
        } 
    finally {
            
    rs.closeRecordStore();
        }
        return 
    result;

    Note that this version adds a closeRecordStore(), and this is in a finally{} block, which ensures that the close will always happen.

    Also, there is no catch{} block. Any exception will be passed back to the caller. This is much more useful than returning "null" or an incomplete result vector, since the exception contains information about what went wrong.

    Graham.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    5

    Unhappy Re: Record store does not work on mobile phone

    Dear Graham,

    I changed my code.However I did not work again

    public boolean save() throws RecordStoreException {
    RecordStore rs = null;
    try {
    rs = RecordStore.openRecordStore("myNotes", true);


    } catch (RecordStoreException rse) {
    System.err.println(rse.toString());
    return false;
    }
    byte[] elementToBeSaved = this.toByte();

    try {
    rs.addRecord(elementToBeSaved, 0, elementToBeSaved.length);

    } finally {
    rs.closeRecordStore();
    }

    return true;
    }

    public static Vector getStoredShoppingLists() throws RecordStoreException {
    Vector records = new Vector();
    Vector result = new Vector();
    RecordStore rs = null;
    rs = RecordStore.openRecordStore("myNotes", true);
    try {
    int num = rs.getNumRecords();
    for (int i = 1; i <= num; i++) {
    byte[] recordAsByte = rs.getRecord(i);
    records.addElement(new String(recordAsByte));
    }

    } finally {
    rs.closeRecordStore();
    }

    for (int i = 0; i < records.size(); i++) {
    String record = (String) records.elementAt(i);
    result.addElement(getMyNotes(record));
    }
    return result;
    }
    Please help!!

    Thanks
    Last edited by shv_rk; 2009-08-14 at 17:59.

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Record store does not work on mobile phone

    Try this:

    PHP Code:
    import java.io.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.*;
    import javax.microedition.rms.*;

    public class 
    Rms extends MIDlet implements CommandListener {
        private static final 
    String RS_NAME "test";
        private static final 
    String ENCODING "UTF-8";
        private static final 
    Command CMD_SAVE = new Command("Save"Command.SCREEN1);
        private static final 
    Command CMD_EXIT = new Command("Exit"Command.EXIT, 1);

        private 
    TextBox text;
        
        public 
    void startApp() {
            
    String message null;
            if (
    text == null) {
                
    String s;
                try {
                    
    read();
                } catch (
    Exception e) {
                    
    "(error)";
                    
    message e.toString();
                }
                
    text = new TextBox("Test"s256TextField.ANY);
                
    text.addCommand(CMD_SAVE);
                
    text.addCommand(CMD_EXIT);
                
    text.setCommandListener(this);
            }
            
    Display display Display.getDisplay(this);
            if (
    message == null) {
                
    display.setCurrent(text);
            } else {
                
    Alert alert = new Alert("Rms"messagenullAlertType.ERROR);
                
    display.setCurrent(alerttext);
            }
        }
        
        public 
    void destroyApp(boolean b){}
        
        public 
    void pauseApp(){}

        public 
    void commandAction(Command cDisplayable d) {
            if (
    == CMD_SAVE) {
                
    String message;
                
    AlertType type;
                try {
                    
    write(text.getString());
                    
    message "Saved.";
                    
    type AlertType.CONFIRMATION;
                } catch (
    Exception e) {
                    
    message e.toString();
                    
    type AlertType.ERROR;
                }
                
    Alert alert = new Alert("Rms"messagenulltype);
                
    Display.getDisplay(this).setCurrent(alert);
            } else if (
    == CMD_EXIT) {
                
    notifyDestroyed();
            }
        }

        private static 
    String read() throws RecordStoreExceptionUnsupportedEncodingException {
            
    String s;
            
    RecordStore rs RecordStore.openRecordStore(RS_NAMEtrue);
            try {
                if (
    rs.getNumRecords() > 0) {
                    
    byte[] data rs.getRecord(1);
                    
    = new String(dataENCODING);
                } else {
                    
    "(no data)";
                }
            } 
    finally {
                
    rs.closeRecordStore();
            }
            return 
    s;
        }

        private static 
    void write(String sthrows RecordStoreExceptionUnsupportedEncodingException {
            
    byte[] data s.getBytes(ENCODING);
            
    RecordStore rs RecordStore.openRecordStore(RS_NAMEtrue);
            try {
                if (
    rs.getNumRecords() == 0) {
                    
    rs.addRecord(data0data.length);
                } else {
                    
    rs.setRecord(1data0data.length);
                }
            } 
    finally {
                
    rs.closeRecordStore();
            }
        }

    Graham.

  5. #5
    Regular Contributor
    Join Date
    Jan 2008
    Posts
    387

    Re: Record store does not work on mobile phone

    hi, it is because you are deleting rs before you exit some where in your code. else its works fine.
    Thanks and Regards,
    Manjul.

  6. #6
    Nokia Developer Champion
    Join Date
    Sep 2008
    Location
    Noida, U.P.
    Posts
    1,330

    Thumbs up Re: Record store does not work on mobile phone

    Quote Originally Posted by shv_rk View Post
    Dear Graham,

    I changed my code.However I did not work again

    public boolean save() throws RecordStoreException {
    RecordStore rs = null;
    try {
    rs = RecordStore.openRecordStore("myNotes", true);


    } catch (RecordStoreException rse) {
    System.err.println(rse.toString());
    return false;
    }
    byte[] elementToBeSaved = this.toByte();

    try {
    rs.addRecord(elementToBeSaved, 0, elementToBeSaved.length);

    } finally {
    rs.closeRecordStore();
    }

    return true;
    }

    public static Vector getStoredShoppingLists() throws RecordStoreException {
    Vector records = new Vector();
    Vector result = new Vector();
    RecordStore rs = null;
    rs = RecordStore.openRecordStore("myNotes", true);
    try {
    int num = rs.getNumRecords();
    for (int i = 1; i <= num; i++) {
    byte[] recordAsByte = rs.getRecord(i);
    records.addElement(new String(recordAsByte));
    }

    } finally {
    rs.closeRecordStore();
    }

    for (int i = 0; i < records.size(); i++) {
    String record = (String) records.elementAt(i);
    result.addElement(getMyNotes(record));
    }
    return result;
    }
    Please help!!

    Thanks
    Have u got any exception. If yes then please mention it. As Graham already suggested u that close the recordstore well after taking use of it as early as possible to make ur data consistent and durable. Also as manjul said check that have u delete the recordstore somewhere.

    Also please mention the meaning of bold lines. Have u take use of some stream. what u want and what u follow is quite ambiguous. Please mention in detail.
    thanks,
    jitu_goldie..

    KEEP TRYING..

Similar Threads

  1. work fine on emultaor but on mobile phone not
    By stein in forum Mobile Java General
    Replies: 1
    Last Post: 2008-03-25, 08:37
  2. Replies: 6
    Last Post: 2007-11-20, 13:09
  3. Mobile Podcasting - will it happen?
    By mobile monte in forum News and Announcements
    Replies: 0
    Last Post: 2006-09-22, 23:45
  4. Nokia Image Converter
    By davidpurdie in forum General Development Questions
    Replies: 0
    Last Post: 2004-02-18, 15:31

Posting Permissions

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