Salve a tutti, volevo chiedere aiuto per un programma che sto realizzando in j2me. Il problema che mi si pone davanti è il seguente. Quando apro la midlet da me creata(ovvero nel momento in cui la midlet cerca di leggere dati, se possibile, dal record store) e quando tento di salvare i dati da me inseriti nel record store, viene generata un'eccezione che riesco a comprendere, ma che non capisco come risolvere.
MIDlet:
codice:
/*
* Coded by ScemoScemo
* MyMoney - Gestione monete
* MIDlet il cui fine è facilitare la gestione delle monete in mio possesso.
* Consente di gestire tre tipi di monete:
* - 2 euro
* - 1 euro
* - 0,50 euro
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Date;
public class MoneyMidlet extends MIDlet implements CommandListener {
// Componenti Grafici
private Display display;
private Form formPrincipale;
private Command cmdExit;
private Command cmdSave;
private StringItem strDataInizio;
private StringItem credits;
private TextField txtMoneteDa2;
private TextField txtMoneteDa1;
private TextField txtMoneteDa50;
// Componenti per i dati
private Money monete;
public MoneyMidlet() {
display = Display.getDisplay(this);
// Istanzio i vari oggetti del form
formPrincipale = new Form("MyMoney - Gestione Monete");
cmdExit = new Command("Esci", Command.EXIT, 1);
cmdSave = new Command("Salva", Command.OK, 2);
txtMoneteDa2 = new TextField("Monete Da 2:", null, 5, TextField.NUMERIC);
txtMoneteDa1 = new TextField("Monete Da 1:", null, 5, TextField.NUMERIC);
txtMoneteDa50 = new TextField("Monete Da 50:", null, 5, TextField.NUMERIC);
strDataInizio = new StringItem("Data Inizio Raccolta: ", "");
credits = new StringItem("Autore del Programma:", "ScemoScemo");
// Istanzio l'oggetto per la gestione dei dati
monete = new Money();
// Se è la prima volta che viene avviata la midlet setto tutto a 0
if (monete.isFirstTime()) {
strDataInizio.setText(new Date().toString());
txtMoneteDa2.setString("0");
txtMoneteDa1.setString("0");
txtMoneteDa50.setString("0");
} else {
loadFromRS();
}
}
public void loadFromRS() {
strDataInizio.setText(monete.getDataInizioRaccolta());
txtMoneteDa2.setString(Integer.toString(monete.getMoneteDa2()));
txtMoneteDa1.setString(Integer.toString(monete.getMoneteDa1()));
txtMoneteDa50.setString(Integer.toString(monete.getMoneteDa50()));
}
protected void startApp() throws MIDletStateChangeException {
// Aggiungo al form i vari componenti
formPrincipale.append(credits);
formPrincipale.append(strDataInizio);
formPrincipale.append(txtMoneteDa2);
formPrincipale.append(txtMoneteDa1);
formPrincipale.append(txtMoneteDa50);
formPrincipale.addCommand(cmdExit);
formPrincipale.addCommand(cmdSave);
formPrincipale.setCommandListener(this);
// Setto formPrincipale sul display
display.setCurrent(formPrincipale);
}
protected void pauseApp() {
notifyPaused();
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {}
public void commandAction(Command c, Displayable arg1) {
if (c == cmdExit) {
notifyDestroyed();
} else if (c == cmdSave) {
monete.setDataInizioRaccolta(strDataInizio.getText());
monete.setMoneteDa2(Integer.parseInt(txtMoneteDa2.getString()));
monete.setMoneteDa1(Integer.parseInt(txtMoneteDa1.getString()));
monete.setMoneteDa50(Integer.parseInt(txtMoneteDa50.getString()));
monete.saveToRS();
}
}
}
Classe utilizzata dalla MIDlet:
codice:
/*
* Coded by ScemoScemo
* MyMoney - Gestione monete
* MIDlet il cui fine è facilitare la gestione delle monete in mio possesso.
* Consente di gestire tre tipi di monete:
* - 2 euro
* - 1 euro
* - 0,50 euro
*/
import javax.microedition.rms.*;
public class Money {
private RecordStore dataBase = null;
final static private String DBNAME = "mymoney_db";
private int nMonete2;
private int nMonete1;
private int nMonete50;
private String dataInizioRaccolta;
private boolean firstTime;
public Money() {
byte buff[] = null;
firstTime = true;
openRS();
// Leggo la quantità di monete e la data di inzio della raccolta
try {
if (dataBase.getNumRecords() > 0) {
try {
dataBase.getRecord(0, buff, 0);
dataInizioRaccolta = new String(buff);
dataBase.getRecord(1, buff, 0);
nMonete2 = Integer.parseInt(new String(buff));
dataBase.getRecord(2, buff, 0);
nMonete1 = Integer.parseInt(new String(buff));
dataBase.getRecord(3, buff, 0);
nMonete50 = Integer.parseInt((new String(buff)));
//closeRS();
} catch (RecordStoreNotOpenException e) {
e.printStackTrace();
} catch (InvalidRecordIDException e) {
e.printStackTrace();
} catch (RecordStoreException e) {
e.printStackTrace();
}
firstTime = false;
}
} catch (RecordStoreNotOpenException e) {
e.printStackTrace();
}
}
// Metodi GETTER
public int getMoneteDa2() { return nMonete2; }
public int getMoneteDa1() { return nMonete1; }
public int getMoneteDa50() { return nMonete50; }
public String getDataInizioRaccolta() { return dataInizioRaccolta; }
/**
* @return true se è la prima volta che viene avviata la midlet, false altrimenti
*/
public boolean isFirstTime() { return firstTime; }
// Metodi SETTER
public void setDataInizioRaccolta(String dataInizioRaccolta) {
this.dataInizioRaccolta = dataInizioRaccolta;
}
public void setMoneteDa2(int moneteDa2) { nMonete2 = moneteDa2; }
public void setMoneteDa1(int moneteDa1) { nMonete1 = moneteDa1; }
public void setMoneteDa50(int moneteDa50) { nMonete50 = moneteDa50; }
public void saveToRS() {
byte[] buff;
/* Se non è aperto lo apro, altrimento lo elimino e lo ricreo */
if (RecordStore.listRecordStores() == null) {
openRS();
} else {
closeAndDeleteRS();
openRS();
}
// Tento di scrivere nel record store
try {
buff = dataInizioRaccolta.getBytes();
dataBase.addRecord(buff, 0, buff.length);
buff = (Integer.toString(nMonete2)).getBytes();
dataBase.addRecord(buff, 0, buff.length);
buff = (Integer.toString(nMonete1)).getBytes();
dataBase.addRecord(buff, 0, buff.length);
buff = (Integer.toString(nMonete50)).getBytes();
dataBase.addRecord(buff, 0, buff.length);
closeRS();
} catch (InvalidRecordIDException e) {
e.printStackTrace();
} catch (RecordStoreNotOpenException e) {
e.printStackTrace();
} catch (RecordStoreFullException e) {
e.printStackTrace();
} catch (RecordStoreException e) {
e.printStackTrace();
}
}
private void openRS() {
// Tento di aprire il record store
try {
dataBase = RecordStore.openRecordStore(DBNAME, true);
} catch (RecordStoreException rse) {
rse.printStackTrace();
}
}
private void closeRS() {
// Tento di chiudere il RecordStore
try {
dataBase.closeRecordStore();
}
catch (RecordStoreException e) {
e.printStackTrace();
}
}
private void closeAndDeleteRS() {
// Tento di chiudere ed eliminare il RecordStore
try {
dataBase.closeRecordStore();
RecordStore.deleteRecordStore(DBNAME);
}
catch (RecordStoreException e) {
e.printStackTrace();
}
}
}
Eccezioni Generate:
codice:
Questa mi indica che l'id passatogli non è valido, però io specifico che se il record store è vuoto non devo accederglil.
javax.microedition.rms.InvalidRecordIDException
at javax.microedition.rms.RecordStore.findRecord(+98)
at javax.microedition.rms.RecordStore.getRecord(+18)
at Money.<init>(+40)
at MoneyMidlet.<init>(+142)
at java.lang.Class.runCustomCode(+0)
at com.sun.midp.midlet.MIDletState.createMIDlet(+34)
at com.sun.midp.midlet.Selector.run(+22)
codice:
Questa mi indica che non c'è un riferimento giusto per un oggetto da quanto ho capito.
java.lang.NullPointerException
at Money.saveToRS(+33)
at MoneyMidlet.commandAction(+95)
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.handleVmEvent(+186)
at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(+57)