We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

J2ME Simple RMS Database

theakst
theakst asked
on
Medium Priority
1,070 Views
Last Modified: 2013-11-23

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
Comment
Watch Question

Author

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

Commented:
First, the null in your string comes from here...

private String tempRec = ""; // initialize to an empty string
Commented:
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Tks bglodde!

Commented:
You're welcome, sorry for my stupid answer earlier...that's what I get for multitasking :/
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.