aspose file tools*
The moose likes Java Micro Edition and the fly likes RMS question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "RMS question" Watch "RMS question" New topic
Author

RMS question

pingkan paula
Ranch Hand

Joined: Sep 03, 2004
Posts: 117
Hi , I'm making a diary appointment application where user can set the time&date which will be stored , updated , and deleted using RMS.
However , when i run the program it launch an exceptions that appears that
the record ID not detected , when i check the program I can't find anything wrong with it

Here is the code that read the recordstore,
rs = RecordStore.openRecordStore("myappt", true);
for(int j=1;j<=rs.getNumRecords();j++){

name=new String("");
at=new String("");
bais=new ByteArrayInputStream(rs.getRecord(j));
dis=new DataInputStream(bais);
str=dis.readUTF();
if(!(str.startsWith("@"))){
for(int i=0;i<str.length();i++){
if(Character.isDigit(str.charAt(i)))
at += str.charAt(i);
else
name+=str.charAt(i);
}
}
long time=0;
String setAt;
if(!(at.trim().equals("")))
{
time = Long.parseLong(at);

java.util.Date date = new java.util.Date(time);
java.util.Calendar rightnow = java.util.Calendar.getInstance();
rightnow.setTime(date);
String year = String.valueOf(rightnow.YEAR);
String month = String.valueOf(rightnow.MONTH);
String day = String.valueOf(rightnow.DATE);
String am = "";
if (rightnow.get(java.util.Calendar.AM_PM) == 0)

am = "PM";

else if (rightnow.get(java.util.Calendar.AM_PM) == 1)

am = "AM";

String hr = String.valueOf(java.util.Calendar.HOUR);
String min = String.valueOf(java.util.Calendar.MINUTE);
setAt = name + "" + day + "/" + month + "/" + year + "" + hr + ":" +
min + "" + am + "";

}

else
{
setAt = name;
}

list.append(str,null);
list.append(setAt,null);
cg.append(setAt,null);
//cg.append(str,null);
name=new String("");
at=new String("");
}


And here is the exceptions occured,

javax.microedition.rms.InvalidRecordIDException
at javax.microedition.rms.RecordStore.findRecord(+86)
at javax.microedition.rms.RecordStore.getRecord(+22)
at dbstreaming.Appointment.getAppointment(+73)
at dbstreaming.Appointment.<init>(+176)
at java.lang.Class.runCustomCode(+0)
at com.sun.midp.midlet.MIDletState.createMIDlet(+19)
at com.sun.midp.midlet.Selector.run(+22)

It was once able to display the choicegroups with null value, but after that it only throws exceptions .

Anyone can help?

cheers


paula
Michael Yuan
author
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
Once you delete records from the store, the record IDs are no longer consecutive since the ID of the deleted record is not reused. So, use the iterator in the API to browse the store instead of assuming the record IDs.


Seam Framework: http://www.amazon.com/exec/obidos/ASIN/0137129394/mobileenterpr-20/
Ringful: http://www.ringful.com/
pingkan paula
Ranch Hand

Joined: Sep 03, 2004
Posts: 117
Thanks Michael , the record was found and displayed but
only the text format , the date and time are only displayed by the class name.Is it because record store can only store 2 fields? or the parser went wrong?
How can we make it to more than 2 fields?

regards,
paula
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

We have a "CODE" tag, that can be created by clicking the CODE button,
which is located under the "Add Reply" button, along with URL, BOLD, etc. buttons

This tag will enable you to keep your formatting for your code, like indentation, and makes your code readable.
The way your code is posted makes it difficult to read, and therefore, more difficult for us to help you.

Thanks

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Tomas Conway
Ranch Hand

Joined: Nov 02, 2004
Posts: 45
1) From the code you have above: Your code reads all it's data from one field per record

2) You are parsing this one field to get two fields : 'name' and 'at', which I presume are the data/time fields you refer to.

3) You haven't shown us the code you are now using to write and read the data (use the CODE button).

Note you don't have to do anything special to add another field to your record, just use a second writeUTF() call for the second String, and use a second readUTF() when reading.

E.g. -



Alternatively, there is no reason why you couldn't add a third String to your existing record, but it's impossible for us to determine whether or not there is an error in your parsing code without seeing your code.


Check that you have done the following:

a) Deleted the record store that only contained the 'date/time' and 'class name' data

b) Updated your record writing code to write the extra data

c) Updated your record reading code to read the extra data

If this does not help, then it might be a good idea if you posted you RecordStore reading and writing code here (use the CODE button).
pingkan paula
Ranch Hand

Joined: Sep 03, 2004
Posts: 117
thanks , i got another problem on my program which is a diary appointment displaying the notes, due date and time, the data is displayed on the screen but only the notes , the date and time is appeared by the class name which is java.util.Date.Here is the program:

public void getAppointment(){
list=new ChoiceGroup("Appointments",ChoiceGroup.EXCLUSIVE);
cg=new ChoiceGroup("Appointments",ChoiceGroup.MULTIPLE);

String name;
String at;
String in;
ByteArrayInputStream bais;
DataInputStream dis;
String str;
try {

rs = RecordStore.openRecordStore("jadwal", true);
for(int j=1;j<=rs.getNumRecords();j++){

name=new String("");
at=new String("");
byte b[]=rs.getRecord(j);
bais=new ByteArrayInputStream(rs.getRecord(j));
dis=new DataInputStream(bais);
str=new String(b,0,b.length);
System.out.println(str);
if(!(str.startsWith("@"))){
for(int i=0;i<str.length();i++){
if(Character.isDigit(str.charAt(i)))
at += str.charAt(i);
else
name+=str.charAt(i);
}
}
long time=0;
String setAt;
if(!(at.trim().equals("")))
{
//parse date and time to be displayed in string value
time = Long.parseLong(at);
java.util.Date date = new java.util.Date(time);
java.util.Calendar rightnow = java.util.Calendar.getInstance();
rightnow.setTime(date);
String year = String.valueOf(rightnow.YEAR);

String month = String.valueOf(rightnow.MONTH);
String day = String.valueOf(rightnow.DATE);
String am = "";
if (rightnow.get(java.util.Calendar.AM_PM) == 0)

am = "PM";

else if (rightnow.get(java.util.Calendar.AM_PM) == 1)

am = "AM";

String hr = String.valueOf(java.util.Calendar.HOUR);
String min = String.valueOf(java.util.Calendar.MINUTE);
setAt = name + "" + day + "/" + month + "/" + year + "" + hr + ":" +
min + "" + am + "";
}

else
{
setAt = name;
}
list.append(setAt,null);
cg.append(setAt,null);
name=new String("");
at=new String("");
}
}
catch (Exception exc) {
exc.printStackTrace();
}
}

is there something wrong with the parser for the date and time in the program? I've tried to add java.util.Date and java.util.Calendar from j2sdk 1.4.1 but it still doesn't work.
Tomas Conway
Ranch Hand

Joined: Nov 02, 2004
Posts: 45
If you would like me to help you further please do the following:

1) Post your code using the CODE button only, as Mark instructed.

2) Clarify the problem: You say that your app is supposed to display "the notes, due date and time" and that "the data is displayed". The problem is that "only the notes , the date and time is appeared by the class name"? You're saying that the notes, data and time that are supposed to appear are actually appearing. If this is not what you mean to say then you need to rethink how you are saying it, and rephrase it so that I can understand.

PS You could put a System.out.println() in your code to check that the data you are getting from the RecordStore is as you expect it to be.
[ December 01, 2004: Message edited by: Tomas Conway ]
pingkan paula
Ranch Hand

Joined: Sep 03, 2004
Posts: 117
This is the requested code , i have tried importing other Calendar class to parse date and time but it doesn't work.I also haven't able to delete the data , i don't know what's the problem and i'm clueless , anyone can give me some input i'll be delighted.

paula

Tomas Conway
Ranch Hand

Joined: Nov 02, 2004
Posts: 45
To start with use RecordEnumeration to loop through the records (call rs.enumerateRecords() to get the RecordEnumeration). This won't solve your problems (not all of them anyway) but it will prevent problems later when you succeed in deleting records.

I'll take a closer look at this later when I get a chance.
Tomas Conway
Ranch Hand

Joined: Nov 02, 2004
Posts: 45
It looks like you're getting something like this - "java.util.Date@123afc2" intead of the date and time. I'm assuming that this the problem that you described before.

The problem is happening because of this line of code



The valueOf() method calls the toString() method of the Date object passed to it. toString() is an Object method. The default behaviour of toString() is to return the name of the object followed by the object's hashcode, however any class can override the method to do something more useful, like returning the date and time in a String.

The Date class is part of CLDC. In version 1.0 of CLDC Date did not override toString() (calling the method would give the results you have been getting). CLDC 1.1 added toString() to the Date class, which returns the date and time in a String format (calling the method would give the results you are looking for).

Thus it seems that you are using CLDC 1.0, when you need to be using CLDC 1.1. If you are getting this problem on an emulator then you can simply change the emulator you use, but if you are programming for a CLDC 1.0 device then you have a little problem (you cannot upgrade or change the CLDC in any way).

If you simply need to store, retrieve and display the date/time then one solution would be to store the value returned by getTime() (in the Date class) as a number (long), instead of storing the date/time as a String. You can retrieve the this number from the record store and use it to initialize a Date object (setTime(long)) and you can use the Date object to initialize a DateField object, when you need to display the date and time.



The following code returns a String that specifies the version of CLDC that is being used -


[ December 07, 2004: Message edited by: Tomas Conway ]
pingkan paula
Ranch Hand

Joined: Sep 03, 2004
Posts: 117
Hi, I've made two steps according to your suggestions.First, I build the code in sun wireless toolkit which support CLDC 1.1 which result a ClassNotFoundException ,is it related to the code or the J2ME setting?anyway, i've set the emulator preferences->security->trusted.

Second, I change the code which i made in JBuilder X and set the apptime like this


Is it right? I can't call the method getDate from appDate class , when i run the code it didn't display the date class anymore.Can you give a direct correction to my code ?

thanx a lot
pingkan paula
Ranch Hand

Joined: Sep 03, 2004
Posts: 117
Hi, I've made two steps according to your suggestions.First, I build the code in sun wireless toolkit which support CLDC 1.1 which result a ClassNotFoundException ,is it related to the code or the J2ME setting?anyway, i've set the emulator preferences->security->trusted.

Second, I change the code which i made in JBuilder X and set the apptime like this


Is it right? I can't call the method getDate from appDate class , when i run the code it didn't display the date class anymore.Can you give a direct correction to my code ?

thanx a lot
Tomas Conway
Ranch Hand

Joined: Nov 02, 2004
Posts: 45
That code looks fine. It should work, but remember you have to change your parsing code as well, to extract the date/time (which is now a long something like 1102948142696) from the String, and use that number to initialize a Date object.

BTW: I suggest that you upgrade or configure JBuilderX to use CLDC 1.1, if possible.
[ December 13, 2004: Message edited by: Tomas Conway ]
pingkan paula
Ranch Hand

Joined: Sep 03, 2004
Posts: 117
Hi , I have changed the code as you told , but the result is still the same.
This is the parser code:


Is it correct? I've been trying to install MIDP 2.0 on my JBuilder and it's not succesfull.Some source said that i should use forte for java, do you know where i can download it ?
Tomas Conway
Ranch Hand

Joined: Nov 02, 2004
Posts: 45
In your string parsing code you are converting Calendar constants into Strings. You need to pass the constants (YEAR, MONTH, etc) to the get() method of the Calendar object. The int returned is the value you require (the year, the month number, etc). You can then put the returned values into a String or Strings as you require.
pingkan paula
Ranch Hand

Joined: Sep 03, 2004
Posts: 117
Hi,I've changed the parser code like this


And, the result still unable to display the time & date i had entered. It displayed 18/11/2004 10/12 AM instead for the date and time.
Also, the displayed data are unable to delete, is it because i don't use data mapping algorithm (i.e ByteArrayOutputStream,DataOutputStream)?
Do you know the solution whether i can delete the data? Here's the example code of deleting data .I hope you can help , thanks a lot.

Tomas Conway
Ranch Hand

Joined: Nov 02, 2004
Posts: 45
The date is wrong because you have hard coded the time in milliseconds with the example number I gave you (1102948142696). That number will always give you the same date and time.

In this code ...



... uncomment the first line and remove the second.


The reason that you cannot delete records maybe because you are not using RecordEnumeration to iterate through your records (see my previous post on this thread about RecordEnumeration). A standard for from 0 to getNumRecords() won't always work. In any case you should use RecordEnumeration anywhere in your code that you want to iterate over the records. At least if you did this it would eliminate a possible cause of the problem.
pingkan paula
Ranch Hand

Joined: Sep 03, 2004
Posts: 117
hi, I've installed JBuilder 2005 which support MIDP 2.1 and I've changed my code.When I run it, it only succesfully display the date but the time remains the same.Do you know how I can get the right time from item AppDate?

Tomas Conway
Ranch Hand

Joined: Nov 02, 2004
Posts: 45
The error may be in your updated parser code. Please post your parser code as well, I can look at both at the same time.
pingkan paula
Ranch Hand

Joined: Sep 03, 2004
Posts: 117
This is the parser code

Tomas Conway
Ranch Hand

Joined: Nov 02, 2004
Posts: 45
You're still converting Calendar constants into Strings for the hours and minutes.

Use the Calendar get() method on these constants too.
[ January 12, 2005: Message edited by: Tomas Conway ]
pingkan paula
Ranch Hand

Joined: Sep 03, 2004
Posts: 117
thank's a lot my code finally able to display the right date&time data But there is a few problem, the data which is append to ChoiceGroup class is displayed with one character missing for example if i input data with the note "gathering" it will only display 'athering' so i have to add one more character to "ggathering" and the screen will display 'gathering'.

One more problem is I'm not able to delete the data in random, the last data will always deleted even if I choose the first one. I've tried to modify the code like this

Unfortunately it throws an exceptions when i run it, is it because i don't use classes from java.io to store and access the data?

best regards,

paula
Tomas Conway
Ranch Hand

Joined: Nov 02, 2004
Posts: 45
I think you should post all your code in one big block, again. That'll make it easier for me to see what's going on.
pingkan paula
Ranch Hand

Joined: Sep 03, 2004
Posts: 117
here is the code
Tomas Conway
Ranch Hand

Joined: Nov 02, 2004
Posts: 45


You need to start that loop at 0 not 1.

It looks like you are deleting the first record in the record store returned by the RecordEnumerator.


If you want to get a particular record from a record store then you need to

EITHER

examine the data in each record in the record store until you find the right one

OR

use the RecordFilter interface in conjunction with RecordEnumeration, so that only the record (or records) you are interested in are returned

AND/OR

use the RecordComparator interface in conjunction with RecordEnumeration, so that the records are returned in a pre-determined order.

You can more information on RecordComparator and RecordFilter in the API.
 
Don't get me started about those stupid light bulbs.
 
subject: RMS question