J2ME Simple RMS Database


Hi all,

Am trying to compile a simple RMS database in MIDP 2.0.  Code compiles & runs just fine, however the "confirmedMenu" screen results in a null string, what am I missing??


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

public class RMS_DB extends MIDlet implements CommandListener {
   
    /** Creates a new instance of Visual_RMS_DB */
    public RMS_DB() {
        initializeRMS();
        initialize();
    }
   
    //Ini entry form "dbMenu"
    private Form dbMenu;
    private TextField textField1;
    private ChoiceGroup choiceGroup1;
    private Command exitCommand1;
    private Command screenCommand1;
    //Ini Confirm form "confirmForm"
    private Form confirmMenu;
    private StringItem stringItem1;
    private Command exitCommand2;
    private Command screenCommand2;
    //Ini vars for RMS db
    private RecordStore recordStore;
    private RecordEnumeration recEnum;
    private Vector recVector;
    private int vectorEnum = 1;
    private String tempRec;
    byte b[] = null;    
    private String confirmString = "confirmed string";
    private StringItem stringItem2;
    private Form trial;
    private Alert alert1;
   
    // Called by the system to indicate that a command has been invoked on a particular displayable.
    public void commandAction(Command command, Displayable displayable) {
    // Insert global pre-action code here
        if (displayable == dbMenu) {
            if (command == screenCommand1) {
                // Insert pre-action code here
                addEquity(textField1.getString());
                confirmString = printEquity();
                getDisplay().setCurrent(get_alert1(), get_confirmMenu());
                //getDisplay().setCurrent(get_confirmMenu());
                // Insert post-action code here
            } else if (command == exitCommand1) {
                // Insert pre-action code here
                exitMIDlet();
                // Insert post-action code here
            }
        } else if (displayable == confirmMenu) {
            if (command == exitCommand2) {
                // Insert pre-action code here
                exitMIDlet();
                // Insert post-action code here
            } else if (command == screenCommand2) {
                // Insert pre-action code here
                getDisplay().setCurrent(get_dbMenu());
                // Insert post-action code here
            }
        }
}
   
    private void initialize() {
        // Insert pre-init code here
        getDisplay().setCurrent(get_dbMenu());
        // Insert post-init code here
    }
   
    public Display getDisplay() {                        
        return Display.getDisplay(this);
    }                        
   
    public void exitMIDlet() {                        
        getDisplay().setCurrent(null);
        destroyApp(true);
        notifyDestroyed();
    }                        

    //The Section for the RMS db
    public RecordStore initializeRMS() {
        try {
            recordStore = RecordStore.openRecordStore("db1", true);
            RecordEnumeration recEnum = recordStore.enumerateRecords(null, null, false);
            recVector = new Vector();
           
        } catch (RecordStoreException rse) {
            System.out.println(rse);
          rse.printStackTrace();
        }
        return recordStore;
    }
   
    private void addEquity(String addStock)
    {
        try {
            recordStore.addRecord(addStock.getBytes(), 0, addStock.getBytes().length);
            vectorEnum++;
            getDisplay().setCurrent(get_trial());
           
        } catch (RecordStoreException rse) {
            System.out.println(rse);
          rse.printStackTrace();
        }  
        //return vectorEnum;
    }
   
    private String printEquity()
    {
        try {
           
            for (int counter = 1; counter <= recVector.size(); counter++)
            {
                b = recordStore.getRecord(counter);
                for(int i=0; i <= b.length; i++)        
                {
                    tempRec += b.toString();  
                }
               
            }
           
        } catch (RecordStoreException rse) {
            System.out.println(rse);
          rse.printStackTrace();
        }  
       
        return tempRec;
       
   }
    public Form get_dbMenu() {
        if (dbMenu == null) {
            // Insert pre-init code here
            dbMenu = new Form("Stock Ticker 1.0", new Item[] {
                get_textField1(),
                get_choiceGroup1(),
                get_stringItem2()
            });
            dbMenu.addCommand(get_exitCommand1());
            dbMenu.addCommand(get_screenCommand1());
            dbMenu.setCommandListener(this);
            // Insert post-init code here
        }
        return dbMenu;
    }
   
    public Form get_trial() {
        if (dbMenu == null) {
            // Insert pre-init code here
            dbMenu = new Form("This loop does exit!!");
            dbMenu.addCommand(get_exitCommand1());
        }
        return dbMenu;
    }
   
    public TextField get_textField1() {
        if (textField1 == null) {
            // Insert pre-init code here
            textField1 = new TextField("Stock Symbol:", null, 120, TextField.ANY);
            // Insert post-init code here
        }
        return textField1;
    }

    public ChoiceGroup get_choiceGroup1() {
        if (choiceGroup1 == null) {
            // Insert pre-init code here
            choiceGroup1 = new ChoiceGroup("choiceGroup1", Choice.POPUP, new String[] {
                "NYSE",
                "TSX",
                "MSX",
                "Alberta",
                "Nasdaq",
                "Other"
            }, new Image[] {
                null,
                null,
                null,
                null,
                null,
                null
            });
            choiceGroup1.setSelectedFlags(new boolean[] {
                false,
                false,
                false,
                false,
                false,
                false
            });
            // Insert post-init code here
        }
        return choiceGroup1;
    }

    public Command get_exitCommand1() {
        if (exitCommand1 == null) {
            // Insert pre-init code here
            exitCommand1 = new Command("Exit", Command.EXIT, 1);
            // Insert post-init code here
        }
        return exitCommand1;
    }

    public Command get_screenCommand1() {
        if (screenCommand1 == null) {
            // Insert pre-init code here
            screenCommand1 = new Command("Screen", Command.SCREEN, 1);
            // Insert post-init code here
        }
        return screenCommand1;
    }
   
    public Form get_confirmMenu() {
        if (confirmMenu == null) {
            // Insert pre-init code here
            confirmMenu = new Form("Stock Ticker 1.0 - Confirm Menu", new Item[] {get_stringItem1()});
            confirmMenu.addCommand(get_exitCommand2());
            confirmMenu.addCommand(get_screenCommand2());
            confirmMenu.setCommandListener(this);
            // Insert post-init code here
        }
        return confirmMenu;
    }
   
    public StringItem get_stringItem1() {
        if (stringItem1 == null) {
            // Insert pre-init code here
            stringItem1 = new StringItem("Stocks Confirmed Are:", "\n" + " " + tempRec + " " +
                    confirmString + " "+ "apples");
            // Insert post-init code here
        }
        return stringItem1;
    }
   
    public StringItem get_stringItem2() {
        if (stringItem2 == null) {
            // Insert pre-init code here
            stringItem2 = new StringItem("Stocks Confirmed Are:", "\n" + " " + tempRec + " " +
                    confirmString + " "+ "apples");
        }
        return stringItem2;
    }

    public Command get_exitCommand2() {
        if (exitCommand2 == null) {
            // Insert pre-init code here
            exitCommand2 = new Command("Exit", Command.EXIT, 1);
            // Insert post-init code here
        }
        return exitCommand2;
    }

    public Command get_screenCommand2() {
        if (screenCommand2 == null) {
            // Insert pre-init code here
            screenCommand2 = new Command("Screen", Command.SCREEN, 1);
            // Insert post-init code here
        }
        return screenCommand2;
    }
   
    public void close() throws RecordStoreNotOpenException, RecordStoreException {
        if (recordStore.getNumRecords() == 0) {
            String fileName = recordStore.getName(); //returns the name of the record store into "fileName"
            recordStore.closeRecordStore(); //close the record store
            recordStore.deleteRecordStore(fileName); //deletes the name of the record store "fileName" named above
        } else {
            recordStore.closeRecordStore(); //if the record store has >= 1 records, close the RecordStore
        }
    }
   
     public Alert get_alert1() {
        if (alert1 == null) {
            // Insert pre-init code here
            alert1 = new Alert(null, "Please Wait for Loading", null, AlertType.INFO);
            alert1.setTimeout(2000);
            getDisplay().setCurrent(get_confirmMenu());            
            // Insert post-init code here
        }
        return alert1;
    }
   
    public void startApp() {
    }
   
    public void pauseApp() {
    }
   
    public void destroyApp(boolean unconditional) {
      try {
        close(); //close or delete the record store
      } catch(Exception e) {}
      notifyDestroyed();
    }
   
   
}

IDE is Netbeans 5.0

Thanks in advance!!
Rob
theakstAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

theakstAuthor Commented:

The problem can be simplified into:
 
private void addEquity(String addStock)
    {
        try {
            recordStore.addRecord(addStock.getBytes(), 0, addStock.getBytes().length);
            vectorEnum++;
           
        } catch (RecordStoreException rse) {
            System.out.println(rse);
          rse.printStackTrace();
        }  
    }
   
    private String printEquity()
    {
        try {
                b = recordStore.getRecord(1);
                tempRec = b.toString();
           
        } catch (RecordStoreException rse) {
            System.out.println(rse);
          rse.printStackTrace();
        }          
        return tempRec;    
   }

Where a string is submitted to the addEquity function and is looked up by the printEquity method.  The problem I receive is that the emulator produces the 'Value' of the stored record rather than the string itself.  This has something to do with dereferencing Bytes & Strings in Java.

Please advise!!
Tks
0
bgloddeCommented:
First, the null in your string comes from here...

private String tempRec = ""; // initialize to an empty string
0
bgloddeCommented:
Scratch that - this is what you need:


private void addEquity(String addStock)
    {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream outputStream = new DataOutputStream(baos);

        try
        {
             outputStream.writeUTF(addStock);
             byte[] b = baos.toByteArray();
             recordStore.addRecord(b, 0, b.length);
             vectorEnum++;  
             getDisplay().setCurrent(get_trial());
        }
        catch (RecordStoreException rse)
        {
             System.out.println(rse);
             rse.printStackTrace();
        }
        catch(IOException ioe)
        {
            ioe.printStackTrace();
        }
    }
   
    private String printEquity()
    {
        try
        {
            RecordEnumeration recEnum = recordStore.enumerateRecords(null, null, false);
           
            while(recEnum.hasNextElement())
            {
                int id = recEnum.nextRecordId();
                ByteArrayInputStream bais = new ByteArrayInputStream(recordStore.getRecord(id));
              DataInputStream inputStream = new DataInputStream(bais);
                String rec = inputStream.readUTF();
                System.out.println("name: " + rec);
               
                tempRec += "\n" + rec;
            }
        }
        catch (RecordStoreException rse)
        {
            System.out.println(rse);
            rse.printStackTrace();
        }
        catch(IOException ioe)
        {
            ioe.printStackTrace();
        }
       
        return tempRec;
   }

Hope it helps.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
theakstAuthor Commented:
Tks bglodde!
0
bgloddeCommented:
You're welcome, sorry for my stupid answer earlier...that's what I get for multitasking :/
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.