?
Solved

J2ME Simple RMS Database

Posted on 2006-03-30
5
Medium Priority
?
992 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
0
Comment
Question by:theakst
  • 3
  • 2
5 Comments
 

Author Comment

by:theakst
ID: 16336973

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
 
LVL 13

Expert Comment

by:bglodde
ID: 16337018
First, the null in your string comes from here...

private String tempRec = ""; // initialize to an empty string
0
 
LVL 13

Accepted Solution

by:
bglodde earned 2000 total points
ID: 16337453
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
 

Author Comment

by:theakst
ID: 16337663
Tks bglodde!
0
 
LVL 13

Expert Comment

by:bglodde
ID: 16337678
You're welcome, sorry for my stupid answer earlier...that's what I get for multitasking :/
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question