0

I'm trying write application in Jave ME with RMS. Application stores information about courier's customer.

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.DataOutputStream;
import java.io.DataInputStream;

public class ClientsApp extends MIDlet implements CommandListener {
// controls
private Display screen = null; 
private List menu = null;
private Form addClientForm = null;
private Form showClientForm = null;
private RecordStore clients;
private ByteArrayOutputStream stream = null;
private DataOutputStream out = null;
private byte[] dates;

TextField name = null;
TextField surname = null;
TextField email = null;
TextField phone = null;
DateField date = null;
TextField price = null;
TextField description = null;


// comands
private final Command backCommand;
private final Command mainMenuCommand;
private final Command exitCommand; 
private final Command addClientCommand;

public ClientsApp() {
    // initializating controls and comands
    menu = new List("Lista klientów", Choice.IMPLICIT);
    backCommand = new Command("Cofnij", Command.BACK, 0);
    mainMenuCommand = new Command("Main", Command.SCREEN, 1);
    exitCommand = new Command("Koniec", Command.EXIT, 2);
    addClientCommand = new Command("Zapisz", Command.OK, 3);    
    stream = new ByteArrayOutputStream();
    out = new DataOutputStream(stream);

    menu.append("Dodaj klienta", null);
    menu.append("Przegladaj klientow", null);
    menu.append("Usun klienta", null);
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

}

protected void pauseApp() {

}

protected void startApp() throws MIDletStateChangeException {
    screen = Display.getDisplay(this);
    screen.setCurrent(menu);

    try {
        clients = RecordStore.openRecordStore("clients", false, RecordStore.AUTHMODE_PRIVATE, false);
    }
    catch(RecordStoreException exc) {
    }

    menu.addCommand(exitCommand);
    menu.setCommandListener(this);
}

public void commandAction(Command cmd, Displayable dsp) {
    if(cmd.getCommandType() == Command.EXIT) {
        try{
            destroyApp(false);
            notifyDestroyed();
        }
        catch(Exception exc) {
            exc.printStackTrace();              
        }
    }
    else if(cmd.getCommandType() == Command.BACK) {
        screen.setCurrent(menu);
    }
    else if(cmd.getCommandType() == Command.OK) {
        try {
            out.writeUTF(name.getString());
            out.writeUTF(surname.getString());
            out.writeUTF(email.getString());
            out.writeUTF(phone.getString());
            out.writeUTF(date.getDate().toString());
            out.writeUTF(price.getString());
            out.writeUTF(description.getString());
            dates = stream.toByteArray();

            clients.addRecord(dates, 0, dates.length);

            stream.close();
            out.close();
            clients.closeRecordStore();
        }
        catch(Exception exc) {

        }
    }
    else {
        List option = (List) screen.getCurrent();

        switch(option.getSelectedIndex()) {
        case 0 : {
            addClients();
            break;
        }
        case 1 : {
            showClients();
            break;
        }
        case 2 : {
            deleteClients();
            break;
        }
        }
    }
}

protected void addClients() {
    addClientForm = new Form("Dodaj klienta");
    name = new TextField("Imię klienta", "", 10, TextField.ANY);
    surname = new TextField("Nazwisko klienta", "", 15, TextField.ANY);
    email = new TextField("Email klienta", "", 20, TextField.EMAILADDR);
    phone = new TextField("Numer telefonu", "", 12, TextField.PHONENUMBER);
    date = new DateField("Data dostarczenia", DateField.DATE);
    price = new TextField("Do zapłaty", "", 6, TextField.NUMERIC);
    description = new TextField("Uwagi", "", 50, TextField.ANY);

    addClientForm.append(name);
    addClientForm.append(surname);
    addClientForm.append(email);
    addClientForm.append(phone);
    addClientForm.append(date);
    addClientForm.append(price);
    addClientForm.append(description);
    screen.setCurrent(addClientForm);

    addClientForm.addCommand(backCommand);
    addClientForm.addCommand(addClientCommand);
    addClientForm.setCommandListener(this);
}

protected void showClients() {
    TextBox info = new TextBox("Klienci", null, 100, 0);

    RecordEnumeration iterator = null;
    String str = null;
    byte[] temp = null;

    try {
        iterator = clients.enumerateRecords(null, null, false);

        while(iterator.hasNextElement()) {
            temp = iterator.nextRecord();
        }

        for(int i = 0; i < temp.length; i++) {
            str += (char) temp[i];
        }
        System.out.println(str);
        clients.closeRecordStore();
    }
    catch(Exception exc) {

    }       

    info.setString(str);
    screen.setCurrent(info);
}
}

Write/read information from RecordStore don't work. I don't have any exception throw. Could somebody help me?

PS Sorry for my bad language.

user2316721
  • 19
  • 3
  • 7

1 Answers1

0

Are you sure you do not get any exception? Catch blocks are empty... I see several issues: Shouldn't you open the record store with createIfNecessary (2nd parameter) set to true? In ShowClients method, you should use DataInputStream to read items from the record (the byte array 'temp'), the loop over temp is strange. And a check for null 'temp' to avoid NPE when the store is empty is missing too. On OK command, and also in ShowClients, the store is closed, so next time it will fail with RecordStoreNotOpenException I guess.

I would also consider flushing 'out' stream before calling stream.toByteArray(), although in this case (DataOutputStrea/ByteArrayOutputStream) it is nothing but a good practice..

Ales
  • 168
  • 9