2

I have code to stored the value using RMS in J2ME. It's working fine on emulator. So, my first problem is

  1. When i restart the emulator all the stored values are deleted.

  2. Stored values are showing in the emulator, but not in the Mobile, in which i am testing my application.

I am using NetBeans for developing my J2ME application.

===UPDATED===

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.*;

public class TryNew extends MIDlet implements CommandListener, ItemCommandListener {

    private RecordStore record;
    private StringItem registered;
    static final String REC_STORE = "SORT";
    //Button existUser;
    Display display = null;
    private Ticker ticker;
    Form form = null;
    Form form1 = null;
    TextField tb, tb1, tb2, tb3;
    ChoiceGroup operator = null;
    String str = null;
    Command backCommand = new Command("Back", Command.BACK, 0);
    Command loginCommand = new Command("Login", Command.OK, 2);
    Command saveCommand = new Command("Save New", Command.OK, 1);
    Command sendCommand = new Command("Send", Command.OK, 2);
    Command selectCommand = new Command("Select", Command.OK, 0);
    Command exitCommand = new Command("Exit", Command.STOP, 3);
    private ValidateLogin ValidateLogin;

    public TryNew() {
    }

    public void startApp() throws MIDletStateChangeException {
        display = Display.getDisplay(this);
        form = new Form("Login");
        registered = new StringItem("", "Registered ?", StringItem.BUTTON);
        form1 = new Form("Home");
        tb = new TextField("Login Id: ", "", 10, TextField.PHONENUMBER);//TextField.PHONENUMBER
        tb1 = new TextField("Password: ", "", 30, TextField.PASSWORD);
        operator = new ChoiceGroup("Select Website", Choice.POPUP, new String[]{"Game", "Joke", "SMS"}, null);
        form.append(tb);
        form.append(tb1);
        form.append(operator);
        form.append(registered);
        registered.setDefaultCommand(selectCommand);
        registered.setItemCommandListener(this);
        form.addCommand(saveCommand);
        ticker = new Ticker("Welcome Screen");
        form.addCommand(loginCommand);
        form.addCommand(selectCommand);
        form.addCommand(exitCommand);
        //   existUser = new StringItem(null, "Registered ?");
        // form.append(existUser);
        form.setCommandListener(this);
        form1.addCommand(exitCommand);
        form1.addCommand(sendCommand);
        form1.setCommandListener(this);
        form.setTicker(ticker);
        display.setCurrent(form);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    void showMessage(String message, Displayable displayable) {
        Alert alert = new Alert("");
        alert.setTitle("Error");
        alert.setString(message);
        alert.setType(AlertType.ERROR);
        alert.setTimeout(5000);
        display.setCurrent(alert, displayable);
    }

    public void commandAction(Command c, Displayable d) {
        if (c == exitCommand) {
            destroyApp(true);
            notifyDestroyed();
        } else if (c == backCommand) {
            display.setCurrent(form);
        } else if (c == loginCommand) {
            ValidateLogin = new ValidateLogin(this);
            ValidateLogin.start();
            ValidateLogin.validateLogin(tb.getString(), tb1.getString(), operator.getString(operator.getSelectedIndex()));
        } else if (c == saveCommand) {
            openRecord();
            writeRecord(tb.getString(), tb1.getString(), operator.getString(operator.getSelectedIndex()));
            closeRecord();
            showAlert("Login Credential Saved Successfully !!");
        } 
    }

    ////==============================================================================/////
    /// Record Management
    public void openRecord() {
        try {
            record = RecordStore.openRecordStore(REC_STORE, true);
        } catch (Exception e) {
            db(e.toString());
        }
    }

    public void closeRecord() {
        try {
            record.closeRecordStore();
        } catch (Exception e) {
            db(e.toString());
        }
    }

    public void deleteRecord() {
        if (RecordStore.listRecordStores() != null) {
            try {
                RecordStore.deleteRecordStore(REC_STORE);
            } catch (Exception e) {
                db(e.toString());
            }
        }
    }

    public void writeRecord(String login_id, String pwd, String operator_name) {
        String credential = login_id + "," + pwd + "," + operator_name;
        byte[] rec = credential.getBytes();
        try {
            if (login_id.length() > 10 || login_id.length() < 10) {
                showAlert("Please Enter valid Login Id");
            } else if (pwd.length() < 1) {
                showAlert("Please Password !!");
            } else {
                record.addRecord(rec, 0, rec.length);
            }

        } catch (Exception e) {
            db(e.toString());
        }
    }

    private void showAlert(String err) {
        Alert a = new Alert("");
        a.setString(err);
        a.setTimeout(Alert.FOREVER);
        display.setCurrent(a);
    }

    public void readRecord() {
        try {
            if (record.getNumRecords() > 0) {
                Comparator comp = new Comparator();
                RecordEnumeration re = record.enumerateRecords(null, comp, false);

                while (re.hasNextElement()) {
                    String str = new String(re.nextRecord());
                    showAlert(str);
                }
            }
        } catch (Exception e) {
            db(e.toString());
        }
    }

    private void db(String error) {
        System.err.println("Exception: " + error);
    }

    public void commandAction(Command c, Item item) {
        if (c == selectCommand && item == registered) {
            openRecord();
            readRecord();
            closeRecord();
        }
    }

    class Comparator implements RecordComparator {

        public int compare(byte[] rec1, byte[] rec2) {
            String str1 = new String(rec1);
            String str2 = new String(rec2);
            int result = str1.compareTo(str2);
            if (result == 0) {
                return RecordComparator.EQUIVALENT;
            } else if (result < 0) {
                return RecordComparator.PRECEDES;
            } else {
                return RecordComparator.FOLLOWS;
            }
        }
    }

    class ValidateLogin implements Runnable {

        TryNew midlet;
        private Display display;
        String login_id;
        String pwd;
        String operator_name;

        public ValidateLogin(TryNew midlet) {
            this.midlet = midlet;
            display = Display.getDisplay(midlet);
        }

        public void start() {
            Thread t = new Thread(this);
            t.start();
        }

        public void run() {
            if (login_id.length() > 10 || login_id.length() < 10) {
                showAlert("Please Enter valid Login Id");
            } else if (pwd.length() < 1) {
                showAlert("Please Password !!");
            } else {
                showHome();
            }
        }
        /* This method takes input from user like text and pass
        to servlet */

        public void validateLogin(String login_id, String pwd, String operator_name) {
            this.login_id = login_id;
            this.pwd = pwd;
            this.operator_name = operator_name;
        }

        /* Display Error On screen*/
        private void showAlert(String err) {
            Alert a = new Alert("");
            a.setString(err);
            a.setTimeout(Alert.FOREVER);
            display.setCurrent(a);
        }

        private void showHome() {
            tb2 = new TextField("To: ", "", 30, TextField.PHONENUMBER);
            tb3 = new TextField("Message: ", "", 300, TextField.ANY);
            form1.append(tb2);
            form1.append(tb3);
            form1.addCommand(loginCommand);
            //display.setCurrent(tb3);
            display.setCurrent(form1);

        }
    };
}

This is what i got, when i click the Manage Emulator

Manage Emulator

gnat
  • 6,213
  • 108
  • 53
  • 73

1 Answers1

3

You need to fix the storage_root of your app in WTK,

Whenever you start your enulator it would refer to same storage_root, by default it creates different data files for each session

jmj
  • 237,923
  • 42
  • 401
  • 438
  • How can i do that, because i'm very new in J2ME. Thanks in advance !! –  May 20 '12 at 11:24
  • I don't have WTK installed at this moment, right click on project from netbeans and then there should be some setting called manage emulators check around that there should be an option storage_root, You should see something like [this](http://docs.oracle.com/cd/E17802_01/products/products/sjwtoolkit/wtk2.5.2/docs/UserGuide-html/figures/emulator-7.jpg) – jmj May 20 '12 at 15:08
  • I found the option `manage Emulator`, but didn't found any option like `storage_root` –  May 20 '12 at 16:02
  • Check image and try to find that – jmj May 20 '12 at 17:17
  • i've just uploaded the screenshot, when i'm clicking on `Manage Emulator` –  May 20 '12 at 17:25
  • well then try searching somewhere else may be tools & utilities not sure at this moment – jmj May 20 '12 at 18:25
  • I found one problem there, whenever i restart the emulator, it deleted the *.db file. –  May 20 '12 at 18:27
  • yes that you could also specify in that preference setting window, – jmj May 20 '12 at 18:33