我编写了一个简单的j2me应用程序,用于使用Netbeans 6.8进行订单跟踪。该应用程序允许用户插入新订单,并通过其订单ID搜索订单。该应用程序仅包含一个Midlet,代码如下所示。我还在http://pastie.org/1044069处放置了相同的代码。我在第230行得到错误“ Error:java.lang.IllegalStateException”,它是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());
}
}
}
可能是什么问题呢 ?
请帮忙。
谢谢。
最佳答案
Stacktrace确实会有所帮助。每次都会得到这个例外吗?您可以直接进入searchResultsForm吗?
我可以从您的代码中猜测到的唯一可能的问题是,您将字段添加到两种不同的形式:
首先,要插入OrderForm:
insertOrderForm.append(userId);
比,到searchResultsForm:
searchResultsForm.append(userId);
第二次尝试将引发异常,这是Form contract的一部分:
如果应用程序尝试将某项放入Form中,并且该项已被此Form或另一Form拥有,则抛出IllegalStateException。