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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Inserting records, etc: http://www-128.ibm.com/developerworks/wireless/library/wi-rms/
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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,TextF ield.ANY);
answerB = new TextField("B:","",20,TextF ield.ANY);
answerC = new TextField("C:","",20,TextF ield.ANY);
answerD = new TextField("D:","",20,TextF ield.ANY);
correctAnswer = new TextField("Correct Answer:","",3,TextField.AN Y);
//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.readRecordS tore();
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.addNewRecor d(stquesti on+";"+sta nsA+";"+st AnsB+";"+s tAnsC+";"+ stAnsD+";" +stCorrAns );
System.out.println("Record Added");
}
else if(c == backCommand)
{
Display.getDisplay(Million aire.insta nce).setCu rrent(Mill ionaire.in stance.get Form());
}
}
--------------------------
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.enumerateRecor ds(null, null, false);
while (re.hasNextElement())
{
recordid = re.nextRecordId();
String res = new String(PersistantData.reco rdStore.ge tRecord(re cordid));
res += ";" + recordid;
System.out.println("Record " + ":" + res);
table[i - 1] = res;
i++;
}
return table;
}
catch (Exception e)
{
e.printStackTrace();
return table;
}
}
-------------------------- ---------- -
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,TextF
answerB = new TextField("B:","",20,TextF
answerC = new TextField("C:","",20,TextF
answerD = new TextField("D:","",20,TextF
correctAnswer = new TextField("Correct Answer:","",3,TextField.AN
//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.readRecordS
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.addNewRecor
System.out.println("Record
}
else if(c == backCommand)
{
Display.getDisplay(Million
}
}
--------------------------
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.enumerateRecor
while (re.hasNextElement())
{
recordid = re.nextRecordId();
String res = new String(PersistantData.reco
res += ";" + recordid;
System.out.println("Record
table[i - 1] = res;
i++;
}
return table;
}
catch (Exception e)
{
e.printStackTrace();
return table;
}
}
--------------------------
ASKER
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.enumerateRecor ds(null, null, false);
while (re.hasNextElement())
{
recordid = re.nextRecordId();
String res = new String(PersistantData.reco rdStore.ge tRecord(re cordid));
res += ";" + recordid;
System.out.println("Record " + ":" + res);
table[recNo - 1] = res;
// i++;
}
return table;
}
catch (Exception e)
{
e.printStackTrace();
return table;
}
}
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.enumerateRecor
while (re.hasNextElement())
{
recordid = re.nextRecordId();
String res = new String(PersistantData.reco
res += ";" + recordid;
System.out.println("Record
table[recNo - 1] = res;
// i++;
}
return table;
}
catch (Exception e)
{
e.printStackTrace();
return table;
}
}
ASKER
And this message appear on ToolKit panel...
java.lang.NullPointerExcep tion
at javax.microedition.lcdui.C hoiceGroup .<init>(+9 2)
at javax.microedition.lcdui.C hoiceGroup .<init>(+1 0)
at AddScreen.<init>(+252)
at Millionaire.commandAction( +38)
at javax.microedition.lcdui.D isplay$Dis playAccess or.command Action(+28 2)
at javax.microedition.lcdui.D isplay$Dis playManage rImpl.comm andAction( +10)
at com.sun.midp.lcdui.Default EventHandl er.command Event(+68)
at com.sun.midp.lcdui.Automat edEventHan dler.comma ndEvent(+4 7)
at com.sun.midp.lcdui.Default EventHandl er$QueuedE ventHandle r.run(+250 )
java.lang.NullPointerExcep
at javax.microedition.lcdui.C
at javax.microedition.lcdui.C
at AddScreen.<init>(+252)
at Millionaire.commandAction(
at javax.microedition.lcdui.D
at javax.microedition.lcdui.D
at com.sun.midp.lcdui.Default
at com.sun.midp.lcdui.Automat
at com.sun.midp.lcdui.Default
ASKER
Basically I want to keepp few record... Then after that , I want to retrieve one record from those records kept earlier.
ASKER
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.
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;
Record :asdasasd;as;asd;asd;asd;w
Record :sdfsfs;a;b;c;d;;3
Record :What is the slowest animal in the world?;Duck;Sloth;Cat;Dog;
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.
ASKER
In addition to comment Date: 03/21/2006 05:30AM PST,
I also change the code in AddScreen.java :
form
table = Millionaire.pd.readRecordS tore();
to
table = Millionaire.pd.readOneReco rdStore(2) ;
I also change the code in AddScreen.java :
form
table = Millionaire.pd.readRecordS
to
table = Millionaire.pd.readOneReco
>> Millionaire.pd.addNewRecor d
Can we see that code? Also, what is Millionaire.instance and what is Millionaire.pd?
Can we see that code? Also, what is Millionaire.instance and what is Millionaire.pd?
ASKER
Its is solve now.. I manage to get one record using getRecord().. from rms.