Hello,
I wrote a simple j2me app for order tracking using Netbeans 6.8. The app allows the user to insert a new order, and search for orders by their order id. The app consists of just a single Midlet and the code is shown below. I have also put up the same code at http://pastie.org/1044069 . Im getting an error "Error:java.lang.IllegalStateException" at line 230 which is searchResultsForm.append(userId);
package hello;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Vector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.*;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreNotFoundException;
public class J2MEOrderTracker extends MIDlet implements CommandListener {
private Display display = Display.getDisplay(this);
Form mainForm = new Form("Order Tracker App");
Form searchForm;
StringItem errorMessage = new StringItem("", "");
//UI Text Fields
TextField searchField = new TextField("Search By Order Id", null,25,TextField.ANY);
TextField userId = new TextField("User Id", null,10,TextField.ANY);
TextField customerId = new TextField("Customer Id", "", 10, TextField.ANY);
TextField productName = new TextField("Product Name", "", 40, TextField.ANY);
TextField orderQty = new TextField("Product Qty", "", 2, TextField.NUMERIC);
TextField orderStatus = new TextField("Order Status", "", 2, TextField.ANY);
TextField orderId = new TextField("Order Id", "", 25, TextField.ANY);
//Command buttons
private Command searchOrderButton = new Command("Search Order", Command.OK,1);
private Command searchResultsButton = new Command("Search Results",Command.OK, 1);
private Command insertOrderButton = new Command("Insert New Order",Command.OK, 1);
private Command addOrderButton = new Command("Add Order", Command.OK, 1);
private Command exitButton = new Command("Exit Application", Command.OK, 2);
private Command backButton = new Command("Back",Command.BACK,1);
//Record Store
RecordStore recStore;
//Constants
private static final String ORDER_SHIPPED = "N";
private static final String RECORD_ADDED = "N";
public J2MEOrderTracker(){
mainForm.addCommand(insertOrderButton);
mainForm.addCommand(searchOrderButton);
mainForm.addCommand(exitButton);
mainForm.append(errorMessage);
mainForm.setCommandListener(this);
display.setCurrent(mainForm);
}
private void createDatabase(){
connect();
}
private void connect(){
try {
try {
recStore = RecordStore.openRecordStore("OrderDB",false);
}
catch(RecordStoreNotFoundException re){
}
if (recStore == null) {
//Create new one
recStore = RecordStore.openRecordStore("OrderDB", true);
}
}
catch (Exception e){
System.out.println("Error:"+e);
errorMessage.setLabel("Error:");
errorMessage.setText(e.toString());
}
}
private void closeConnection() {
try{
if(recStore != null){
recStore.closeRecordStore();
}
}
catch(Exception e){
System.out.println("Error:"+e);
errorMessage.setLabel("Error:");
errorMessage.setText(e.toString());
}
}
private String insertRecord(){
String orderId = null;
try{
int recordID = 0;
ByteArrayOutputStream bytstream = new ByteArrayOutputStream();
DataOutputStream writer = new DataOutputStream(bytstream);
//Generate a unique key for the Order Id
long timeStamp = System.currentTimeMillis();
orderId = userId.getString() + String.valueOf(timeStamp);
writer.writeUTF(orderId);
writer.writeUTF(userId.getString());
writer.writeUTF(customerId.getString());
writer.writeUTF(productName.getString());
writer.writeUTF(orderQty.getString());
writer.writeUTF(ORDER_SHIPPED);
writer.writeUTF(RECORD_ADDED);
writer.writeLong(timeStamp);
writer.flush();
byte [] rec = bytstream.toByteArray();
recordID = recStore.addRecord(rec,0,rec.length);
System.out.println("recordID" + recordID);
System.out.println("orderId" + orderId);
writer.close();
bytstream.close();
}
catch(Exception e){
System.out.println("Error:"+e);
errorMessage.setLabel("Error:");
errorMessage.setText(e.toString());
}
return orderId;
}
private Vector fetchData(String orderId){
Vector records = new Vector();
try{
ByteArrayInputStream stream;
DataInputStream reader;
String orderID;
for(int i = 1 ; i <= recStore.getNumRecords() && records.size() == 0 ; i++){
byte [] rec = new byte[recStore.getRecordSize(i)];
rec = recStore.getRecord(i);
stream = new ByteArrayInputStream(rec);
reader = new DataInputStream(stream);
orderID = reader.readUTF();
if(orderID.equals(orderId)){
records.addElement(orderId);
// User Id
records.addElement(reader.readUTF());
// Customer Id
records.addElement(reader.readUTF());
// Product Name
records.addElement(reader.readUTF());
// Productquantity
records.addElement(reader.readUTF());
// Order status
records.addElement(reader.readUTF());
// sync status
records.addElement(reader.readUTF());
// order create date
records.addElement(reader.readUTF());
// record id
records.addElement(new Integer(i));
}
}
}
catch(Exception e){
System.out.println("Error:"+e);
errorMessage.setLabel("Error:");
errorMessage.setText(e.toString());
}
return records;
}
public void startApp() {
createDatabase();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
closeConnection();
}
public void commandAction(Command c, Displayable d) {
try {
if(c == exitButton){
destroyApp(false);
notifyDestroyed();
return;
}
else if(c == backButton){
display.setCurrent(mainForm);
mainForm.setCommandListener(this);
return;
}
else if(c == searchOrderButton){
searchForm = new Form("Search Order By Order Id");
searchForm.append(searchField);
searchForm.addCommand(searchResultsButton);
searchForm.addCommand(backButton);
searchForm.addCommand(exitButton);
searchForm.setCommandListener(this);
display.setCurrent(searchForm);
}
else if(c == searchResultsButton){
Form searchResultsForm = new Form("Search Order Results");
Vector results = fetchData(searchField.getString());
if(results != null && results.size() > 0){
orderId.setString((String) results.elementAt(0));
userId.setString((String) results.elementAt(1));
customerId.setString((String) results.elementAt(2));
productName.setString((String) results.elementAt(3));
orderQty.setString((String) results.elementAt(4));
orderStatus.setString((String) results.elementAt(5));
searchResultsForm.append(userId); //Error:java.lang.IllegalStateException
searchResultsForm.append("\n");
searchResultsForm.append(customerId);
searchResultsForm.append("\n");
searchResultsForm.append(productName);
searchResultsForm.append("\n");
searchResultsForm.append(orderQty);
searchResultsForm.append("\n");
searchResultsForm.append(orderStatus);
searchResultsForm.append("\n");
}
else{
searchResultsForm.append("No Results Found !");
}
display.setCurrent(searchResultsForm);
}
else if (c == insertOrderButton)
{
Form insertOrderForm = new Form("Insert Order");
insertOrderForm.addCommand(addOrderButton);
insertOrderForm.addCommand(backButton);
insertOrderForm.addCommand(exitButton);
insertOrderForm.append(userId);
insertOrderForm.append(customerId);
insertOrderForm.append(productName);
insertOrderForm.append(orderQty);
insertOrderForm.setCommandListener(this);
display.setCurrent(insertOrderForm);
}
else if(c == addOrderButton){
Form orderIdForm = new Form("Order Information");
String orderId = insertRecord();
orderIdForm.append("Order successfully inserted.Order Id is "+orderId);
orderIdForm.addCommand(searchOrderButton);
orderIdForm.addCommand(exitButton);
orderIdForm.setCommandListener(this);
display.setCurrent(orderIdForm);
}
}
catch(Exception e){
System.out.println("Error:"+e);
errorMessage.setLabel("Error:");
errorMessage.setText(e.toString());
}
}
}
What could be the problem ? Please help. Thank You.