We help IT Professionals succeed at work.

maintain record store in J2ME

redcoder
redcoder asked
on
Medium Priority
321 Views
Last Modified: 2008-01-09
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..
Comment
Watch Question

Principal Technologist
CERTIFIED EXPERT
Commented:
Have a look at these:

http://java.sun.com/developer/J2METechTips/2001/tt0319.html

Can you post some code?

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Commented:
http://developers.sun.com/techtopics/mobility/midp/articles/persist/
Let us see what you're doing during your saves/updates.

Author

Commented:
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;
    }
  }
 
-------------------------------------


Author

Commented:
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;
    }
  }

Author

Commented:
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)

Author

Commented:
Basically I want to keepp few record... Then after that , I want to retrieve one record from those records kept earlier.

Author

Commented:
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.

Author

Commented:
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);
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> Millionaire.pd.addNewRecord

Can we see that code? Also, what is Millionaire.instance and what is Millionaire.pd?

Author

Commented:
Its is solve now.. I manage to get one record using getRecord().. from rms.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.