I found an example class for creating my record store and i implemented it in this class:
Code:
import javax.microedition.rms.*;
import java.util.Enumeration;
import java.util.Vector;
import java.io.*;
public class PosDB {
public RecordStore recordStore = null;
public PosDB() {}
// Open a record store with the given name
public PosDB(String fileName) {
try {
recordStore = RecordStore.openRecordStore(fileName, true);
} catch(RecordStoreException rse) {
rse.printStackTrace();
}
}
// Close the record store
public void close()
throws RecordStoreNotOpenException,
RecordStoreException {
if (recordStore.getNumRecords() == 0) {
String fileName =
recordStore.getName();
recordStore.closeRecordStore();
recordStore.deleteRecordStore(
fileName);
} else {
recordStore.closeRecordStore();
}
}
// Add a new record (position)
// to the record store
public synchronized void addNewPos(String record) {
byte[] rec = record.getBytes();
try
{
recordStore.addRecord(rec, 0, rec.length);
}
catch (Exception e)
{
System.out.println(e);
e.printStackTrace();
}
}
// Enumerate through the records.
public synchronized RecordEnumeration enumerate() throws RecordStoreNotOpenException {
return recordStore.enumerateRecords(null, null, false);
}
public synchronized byte[] getPos(int number){
byte[] recData = new byte[2];
try
{
if (recordStore.getRecordSize(number) > recData.length)
recData = new byte[recordStore.getRecordSize(number)];
recData = recordStore.getRecord(number);
}
catch (Exception e)
{
System.out.println(e);
e.printStackTrace();
}
return recData;
}
}
now i think this should be correct for the basic functions of adding to the database etc, so in another class i write
Code:
public void addToDatabase(){
try{
db = new PosDB("positions");
} catch (Exception e){}
try {
db.addNewPos(splitData[2]+splitData[3]);
}catch(Exception e) {}
try{
db.addNewPos(splitData[4]+splitData[5]);
}catch(Exception e) { }
try {
db.close();
} catch(Exception e) {}
return;
}
this should be right yes?
anyway, i then try to output the contents of records 1 and 2 to the screen ina test midlet that's part of my midlet suite:
Code:
public void startApp() throws MIDletStateChangeException{
boolean b;
try{
db = new PosDB("positions");
} catch (Exception e){}
positiona = db.getPos(1);
for(int i=0; i<positiona.length; i++){
position1+=( char )positiona[ i ];
}
positionb = db.getPos(2);
for(int j=0; j<positionb.length; j++){
position2+=( char )positionb[ j ];
}
display = Display.getDisplay(this);
display.setCurrent(form);
form.addCommand(exitCommand);
form.append("position 1:");
form.append(position1 + "\n");
form.append("position 2:");
form.append(position2 + "\n");
try{
db.close();
}
catch(Exception e){
System.out.println(e);
}
but all i get coming out is null. for the life of me i can't figure out why this is happening, i'd normally put in some println statements to make sure everything's being called and data is being save etc, but i cant do that on a moble device and i dont think i can use bluetooth on the emulator... this is my first mobile application and it's abit tough!
can anyone see where i'm going wrong?
sorry for the mammouth post!!!
Andy