将 UI 元素附加到表单时出现 IllegalStateException 错误
我使用 Netbeans 6.8 编写了一个简单的 j2me 应用程序用于订单跟踪。该应用程序允许用户插入新订单,并通过订单 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());
}
}
}
可能是什么问题? 请帮忙。 谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Stacktrace 确实有帮助。你每次都会遇到这个异常吗?可以直接去searchResultsForm吗?
我从您的代码中可以猜测到的唯一可能的问题是您将字段添加到两种不同的表单:
首先,到 insertOrderForm:
,然后到 searchResultsForm:
第二次尝试抛出异常,这是 表单合约:
Stacktrace would really help. Do you get this exception every time? Can you go directly searchResultsForm?
The only possible problem I can guess from your code is that you add your fields to two different forms:
First, to insertOrderForm:
than, to searchResultsForm:
second attempt throws an exception, which is a part of the Form contract: