Link to home
Start Free TrialLog in
Avatar of redcoder
redcoder

asked on

maintain record store in J2ME

In J2ME, why the record store is not kept ? Everytiime i insert the record then close it and build it again and run .. The previous record is gone ... I have to re-insert the record again..
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of redcoder
redcoder

ASKER

So far I am working on insert only..Updating will be later part.

I have class AddScreen.java :

      public AddScreen()
      {
      super("Add Record");
            
      try
      {
            submitCommand = new Command("Submit", Command.OK,0);
            backCommand = new Command("Back", Command.BACK,1);
                  
            //Prepare text field for user to enter
            //question, selection answer and correct answer
            question = new TextField("Question :     ","",50,TextField.ANY);                  
            answerA = new TextField("A:","",20,TextField.ANY);
            answerB = new TextField("B:","",20,TextField.ANY);
            answerC = new TextField("C:","",20,TextField.ANY);
            answerD = new TextField("D:","",20,TextField.ANY);                  
            correctAnswer = new TextField("Correct Answer:","",3,TextField.ANY);
            
            //add back and Submit at soft button
            addCommand(backCommand);
            addCommand(submitCommand);
                  
                  
            append("");
            append(question);
            append(answerA);
            append(answerB);
            append(answerC);
            append(answerD);
            append(correctAnswer);
                  
            setCommandListener(this);
            table = Millionaire.pd.readRecordStore();
            recStore = new ChoiceGroup("", ChoiceGroup.EXCLUSIVE, table, null);
            append(recStore);
      }
      catch (Exception e)
      {
                e.printStackTrace();                  
                }
            
      }

public void commandAction(Command c, Displayable d)
{
if(c == submitCommand)
{
      //assign each input in string datatype
      stquestion = question.getString();
      stansA = answerA.getString();
      stAnsB = answerB.getString();
      stAnsC = answerC.getString();
      stAnsD = answerD.getString();
      stCorrAns = correctAnswer.getString();
                  
      //add record to RecordStore
      Millionaire.pd.addNewRecord(stquestion+";"+stansA+";"+stAnsB+";"+stAnsC+";"+stAnsD+";"+stCorrAns);
System.out.println("Record Added");
}
else if(c == backCommand)
{
Display.getDisplay(Millionaire.instance).setCurrent(Millionaire.instance.getForm());
}
            
}


--------------------------
Then another class PersistantData.java that read the record:

  public String[] readRecordStore()
  {
    String table[] = null;

    try
    {
      int tablesize = recordStore.getNumRecords();
      table = new String[tablesize];
      int recordid;
      int i = 1;
      RecordEnumeration re = recordStore.enumerateRecords(null, null, false);
      while (re.hasNextElement())
      {
        recordid = re.nextRecordId();
        String res = new String(PersistantData.recordStore.getRecord(recordid));
        res += ";" + recordid;
        System.out.println("Record " + ":" + res);
        table[i - 1] = res;
        i++;
      }
      return table;
    }
    catch (Exception e)
    {
      e.printStackTrace();
      return table;
    }
  }
 
-------------------------------------


The code above runs well ... It can show the record kept.. But when I add in the code below , it doesn't show all the records anymore:

  public String[] readOneRecordStore(int recNo)
  {
    String table[] = null;

    try
    {
      int tablesize = recordStore.getNumRecords();
      table = new String[tablesize];
      int recordid;
//      int i = 1;
      RecordEnumeration re = recordStore.enumerateRecords(null, null, false);
      while (re.hasNextElement())
      {
        recordid = re.nextRecordId();
        String res = new String(PersistantData.recordStore.getRecord(recordid));
        res += ";" + recordid;
        System.out.println("Record " + ":" + res);
        table[recNo - 1] = res;
//        i++;
      }
      return table;
    }
    catch (Exception e)
    {
      e.printStackTrace();
      return table;
    }
  }
And this message appear on ToolKit panel...


java.lang.NullPointerException
      at javax.microedition.lcdui.ChoiceGroup.<init>(+92)
      at javax.microedition.lcdui.ChoiceGroup.<init>(+10)
      at AddScreen.<init>(+252)
      at Millionaire.commandAction(+38)
      at javax.microedition.lcdui.Display$DisplayAccessor.commandAction(+282)
      at javax.microedition.lcdui.Display$DisplayManagerImpl.commandAction(+10)
      at com.sun.midp.lcdui.DefaultEventHandler.commandEvent(+68)
      at com.sun.midp.lcdui.AutomatedEventHandler.commandEvent(+47)
      at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(+250)
Basically I want to keepp few record... Then after that , I want to retrieve one record from those records kept earlier.

These are the records I have kept in record store:

Record :Quest 9;Ans 9A;Ans 9B;Ans 9C;Ans 9D;B;9
Record :;;;;;;8
Record :Question 6;answer 1;answer 2;answer 3;answer 4;D;7
Record :Question 6;answer 1;answer 2;answer 3;answer 4;D;6
Record :Who are the pop king;MichealJackson;Elvis;Micheal jordan;David beckham;a;5
Record :asdasasd;as;asd;asd;asd;w;4
Record :sdfsfs;a;b;c;d;;3
Record :What is the slowest animal in the world?;Duck;Sloth;Cat;Dog;B;2
Record :Who create HTML?;Angelie;Barners Lee, Tim;Cynegie;Donald;B;1


Then I want to retrieve the record let say record 1. The number at the end of each record is the resord number. I will base on the number to retrieve the particular record.
In addition to comment Date: 03/21/2006 05:30AM PST,

I also change the code in AddScreen.java :
form
table = Millionaire.pd.readRecordStore();  

to
table = Millionaire.pd.readOneRecordStore(2);
>> Millionaire.pd.addNewRecord

Can we see that code? Also, what is Millionaire.instance and what is Millionaire.pd?
Its is solve now.. I manage to get one record using getRecord().. from rms.