2

1.Problem on emulator:
I am launching my midlet app at first time is to store some data and then I am restarting it at second time is to read the stored data. It is working well in first two cases without any exception.

However I am restarting it second time on the same way then It gives exception: "Uncaught exception java/lang/NumberFormatException:" it is processing only char and total data is less than 64 kb.

2.Problem on real device:
RMS is not working at all. I don't know if I need to give a permission for the handset (nokia n95).

Thanks.


In app, it is only storing charity companies into rms according to a selected country. So if a country is already selected, it must skip country list and then display company list in every restart. In below code, rms_Check() method is to check the data in order to open country or company list frame.

public class X {

private static RecordStore rs =null;
private static Vector rms_Vector = new Vector();
static final String REC_STORE ="db_1";

public X() {

}

public void openRecStore(){
    try {
        rs = RecordStore.openRecordStore(REC_STORE, true);
        System.out.println("open record store");
    } catch (Exception e)
    {
        db(e.toString()+" in openRecStore");
    }
}

public void closeRecStore(){
    try {
        rs.closeRecordStore();
    } catch (Exception e) {
        db(e.toString()+" in closeRecStore");
    }
}

public void deleteRecStore()
{
if (RecordStore.listRecordStores()!=null){
    try {
        RecordStore.deleteRecordStore(REC_STORE);
    } catch (Exception e) {
        db(e.toString()+" in deleteRecStore");
    }
}
}

public void writeRecord(String str) throws UnsupportedEncodingException
{
    byte[] rec = str.getBytes("UTF-8");

    try {
        rs.addRecord(rec, 0, rec.length);
        System.out.println("write record store");
    } catch (Exception e) {
        db(e.toString()+" in writeRecord");
    }
}

public void readRecord()
{
    try {
        // Intentionally it is too small to test code 
        byte[] m_enc = new byte[5];
        byte[] recData = new String(m_enc).getBytes("UTF-8");
        int len;

        for(int i =1; i<= rs.getNumRecords(); i++){
        if(rs.getRecordSize(i)> recData.length)
            recData = new byte[rs.getRecordSize(i)];

        len = rs.getRecord(i, recData, 0);
        System.out.println("Record #"+i+":"+new String(recData, 0, len));
        System.out.println("------------------------");
        rms_Vector.addElement(new String(recData, 0, len));
        }
    } catch (Exception e) {
        db(e.toString() +" in readStore");
    }
}

private void db(String str)
{
System.out.println("Msg:"+str);
}
public Vector rms_Array(){

    return this.rms_Vector;
}

public boolean rms_Check(){
if(this.rms_Vector.size()>0){
    System.out.print("rms_check: true");
    // if true it will display company list every time
return true;
}else{
    System.out.print("rms_check: false");
    //if false it will display country list then company list
return false;
}
}

}
  • could you share problematic code? preferably in [SSCCE](http://www.sscce.org/) form. As for permissions, it is quite unlikely for routine RMS actions to require permissions – gnat Jan 05 '12 at 20:36
  • Please post the code so we will understand wheere exactly problem? – Mr. Sajid Shaikh Jan 06 '12 at 06:03
  • 2
    I shared RMS part of the code and edited my question,I hope it is more understandable now. Thanks. – Hunterman_61 Jan 06 '12 at 10:41
  • Any Help ? is the problem openRecRecord() restriction on restart? – Hunterman_61 Jan 09 '12 at 17:53
  • 1
    I found the problem was emulator, it is working well on BB devices if the data option is allowed on the installation. – Hunterman_61 Feb 13 '12 at 01:27
  • 1
    Nice work Hunterman_61. As a friendly reminder, can you please post an answer to the question yourself and then accept that answer so that we can close this question? Also, you need to accept answers to previous questions if they fix your problem. – Zecas May 28 '12 at 14:23

1 Answers1

1

Use this

private RecordStore rs = null;    // Record store
public String REC_STORE = "RSM name"; // Name of record store
public int record_max=0;

public void openRecStore(){
try{
rs = RecordStore.openRecordStore(REC_STORE, true );
}catch (Exception e){}
}  

 public void closeRecStore(){
try{
rs.closeRecordStore();
}catch (Exception e){}
}

public void deleteRecStore(){
if (RecordStore.listRecordStores() != null){
try{
RecordStore.deleteRecordStore(REC_STORE);
}catch (Exception e){}
}  
}

public void writeRecord(String str){
byte[] rec = str.getBytes();

 try{
 rs.addRecord(rec, 0, rec.length);
 }catch (Exception e){}
 }

  public void readRecords(){
   try{
   byte[] recData = new byte[5]; 
    int len;
    record_max=rs.getNumRecords();
    for(int i = 1; i <= record_max; i++){

        if(rs.getRecordSize(i) > recData.length){
        recData = new byte[rs.getRecordSize(i)];

        }
        len = rs.getRecord(i, recData, 0);
        file_name[i]=new String(recData, 0, len);

       }
     }catch (Exception e){}
   }

you have file_name[] array of save data

for load actin commad use :

            openRecStore();
            readRecords();
            for(int j=1;j<=record_max;j++ ) {
             System.out.println("Record " + j + " : " + file_name[j]);
                }
             closeRecStore();

and save this :

            openRecStore();
            writeRecord(textField.getString());
            closeRecStore();
Dadmand
  • 137
  • 1
  • 5