problems in recordstore

Hi all,

I am completely new to java/j2me. I am trying to write and read to and from a recordstore. The problems that i am facing are

1. If I add both the exit and start command then then none of the command works.

2. If I comment out the exit command line then probably the data gets written into the recordstore but while reading it I print them then it only displays the integer as 0 instead of the value that was assigned to it.

In the code I have marked out the problems I am facing

import javax.microedition.rms.*;

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

import java.io.*;

public class RecordStoreTest

extends MIDlet implements CommandListener

{

private Display display;

private Alert alert;

private Form form;

private Command exit;

private Command start;

private RecordStore recordstore = null;

private RecordEnumeration recordEnumeration = null;

public RecordStoreTest()

{

display = Display.getDisplay(this);

exit = new Command("Exit", Command.SCREEN, 1);

start = new Command("Start", Command.SCREEN, 1);

form = new Form("Mixed Record");

// If I uncomment the line below then none of the Commands work.

//form.addCommand(exit);

form.addCommand(start);

form.setCommandListener(this);

}

public void startApp()

{

display.setCurrent(form);

}

public void pauseApp()

{

}

public void destroyApp( boolean unconditional )

{

}

public void commandAction(Command command, Displayable displayable)

{

if (command == exit)

{

destroyApp(true);

notifyDestroyed();

}

else if (command == start)

{

try

{

recordstore = RecordStore.openRecordStore("myRecordStore", true);

}

catch(Exception error)

{

alert = new Alert("Error Creating", error.toString(), null, AlertType.WARNING);

alert.setTimeout(Alert.FOREVER);

display.setCurrent(alert);

}

try

{

byte[] outputRecord;

String outputString[] = { "ABC", "XYZ", "PQRS" , "UVW", "OTHERS"};

int outputno[] = {1234, 1567, 1890, 1345, 1789};

int outputval[] = {5000, 6000, 7000, 8000, 9000};

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

DataOutputStream outputDataStream = new DataOutputStream(outputStream);

for(int x=0; x<5; x++)

{

outputDataStream.writeInt(outputno[x]);

outputDataStream.writeUTF(outputString[x]);

outputDataStream.writeInt(outputval[x]);

outputDataStream.flush();

outputRecord = outputStream.toByteArray();

recordstore.addRecord(outputRecord, 0, outputRecord.length);

}

outputStream.reset();

outputStream.close();

outputDataStream.close();

}

catch(Exception error)

{

alert = new Alert("Error Writing", error.toString(),null,AlertType.WARNING);

alert.setTimeout(Alert.FOREVER);

display.setCurrent(alert);

}

try

{

StringBuffer buffer = new StringBuffer();

byte[] byteInputData = new byte[300];

ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);

DataInputStream inputDataStream = new DataInputStream(inputStream);

recordEnumeration = recordstore.enumerateRecords(null,null,false);

while(recordEnumeration.hasNextElement())

{

recordstore.getRecord(recordEnumeration.nextRecordId());

// this line prints 0 not the data 1234 written in the recordstore.

System.out.println("Data 1 is : " + inputDataStream.readInt());

buffer.append(inputDataStream.readInt());

buffer.append(" ");

buffer.append(inputDataStream.readUTF());

buffer.append(" ");

buffer.append(inputDataStream.readInt());

buffer.append(" ");

}

}

catch(Exception error)

{

alert = new Alert("Error Reading", error.toString(),null,AlertType.WARNING);

alert.setTimeout(Alert.FOREVER);

display.setCurrent(alert);

}

}

}

}

Thanks a lot

Ayan

[4247 byte] By [ayan_sena] at [2007-11-27 10:02:21]
# 1

Hi Ayen

Your 1st Problem I can find no reason for, I tested using Sun WTK25 and on a Nokia 6680 And The Start and exit buttons both worked.

2nd Problem, You were not reading the records into your byte variable.

I have changed the code - and it works on wtk25 and nokia 6680

import javax.microedition.rms.*;

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

import java.io.*;

public class RecordStoreTest

extends MIDlet implements CommandListener

{

private Display display;

private Alert alert;

private Form form;

private Command exit;

private Command start;

private RecordStore recordstore = null;

private RecordEnumeration recordEnumeration = null;

public RecordStoreTest()

{

display = Display.getDisplay(this);

exit = new Command("Exit", Command.SCREEN, 1);

start = new Command("Start", Command.SCREEN, 1);

form = new Form("Mixed Record");

// If I uncomment the line below then none of the Commands work.

// I tested this using sun WTK 25 and a Nokia 6680 and it works correctly, what are you running the app on?

form.addCommand(exit);

form.addCommand(start);

form.setCommandListener(this);

}

public void startApp()

{

display.setCurrent(form);

}

public void pauseApp()

{

}

public void destroyApp( boolean unconditional )

{

}

public void commandAction(Command command, Displayable displayable)

{

if (command == exit)

{

destroyApp(true);

notifyDestroyed();

}

else if (command == start)

{

try

{

recordstore = RecordStore.openRecordStore("myRecordStore", true);

}

catch(Exception error)

{

alert = new Alert("Error Creating", error.toString(), null, AlertType.WARNING);

alert.setTimeout(Alert.FOREVER);

display.setCurrent(alert);

}

try

{

byte[] outputRecord;

String outputString[] = { "ABC", "XYZ", "PQRS" , "UVW", "OTHERS"};

int outputno[] = {1234, 1567, 1890, 1345, 1789};

int outputval[] = {5000, 6000, 7000, 8000, 9000};

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

DataOutputStream outputDataStream = new DataOutputStream(outputStream);

for(int x=0; x<5; x++)

{

outputDataStream.writeInt(outputno[x]);

outputDataStream.writeUTF(outputString[x]);

outputDataStream.writeInt(outputval[x]);

outputDataStream.flush();

outputRecord = outputStream.toByteArray();

recordstore.addRecord(outputRecord, 0, outputRecord.length);

}

outputStream.reset();

outputStream.close();

outputDataStream.close();

}

catch(Exception error)

{

alert = new Alert("Error Writing", error.toString(),null,AlertType.WARNING);

alert.setTimeout(Alert.FOREVER);

display.setCurrent(alert);

}

try

{

//StringBuffer buffer = new StringBuffer();

String sbuffer="";

byte[] byteInputData = new byte[300];

ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);

DataInputStream inputDataStream = new DataInputStream(inputStream);

recordEnumeration = recordstore.enumerateRecords(null,null,false);

while(recordEnumeration.hasNextElement())

{

//You did not get your data into the byte variable

recordstore.getRecord(recordEnumeration.nextRecordId(),byteInputData,0);

//System.out.println("Data 1 is : " + inputDataStream.readInt());

StringBuffer buffer = new StringBuffer();

buffer.append(inputDataStream.readInt());

buffer.append(" ");

buffer.append(inputDataStream.readUTF());

buffer.append(" ");

buffer.append(inputDataStream.readInt());

buffer.append(" ");

System.out.println("Buffer=" + buffer);

sbuffer=buffer.toString();

form.append(sbuffer.trim() + "\n");

}

}

catch(Exception error)

{

alert = new Alert("Error Reading", error.toString(),null,AlertType.WARNING);

alert.setTimeout(Alert.FOREVER);

display.setCurrent(alert);

}

}

}

}

Hope it helps

Regards

Steve

stevejankoa at 2007-7-13 0:36:39 > top of Java-index,Java Mobility Forums,Java ME Technologies...