PDA

View Full Version : java.io.EOFException ؟



hdv212
دوشنبه 16 مهر 1386, 15:39 عصر
سلام و خسته نباشید
من یه DataBase Application با j2me نوشتم، دفعات اول و دوم درست کار میکرد ولی بعد دیگه هر وقت خواستم رکورد ها رو نمایش بدم با این پیغام خطا مواجه میشم :

java.io.EOFException
ممکنه کدهای منو تست کنید و بگید ایراد کار من کجاست و چرا همچین error ای میده ؟
این کلاسی هست که برای ذخیره ی داده ها به کار میبرم :

import java.io.*;

public class Contact {

private String _firstName;
private String _lastName;
private String _phoneNumber;

public Contact() {
}

public Contact(String firstName,String lastName,String phoneNumber){
_firstName = firstName;
_lastName = lastName;
_phoneNumber = phoneNumber;
}

public String getFirstName(){
return _firstName != null ? _firstName:"";
}

public String getLastName(){
return _lastName != null ? _lastName:"";
}

public String getPhoneNumber(){
return _phoneNumber != null ? _phoneNumber:"";
}

public void setFirstName(String name){
_firstName = name;
}

public void setLastName(String lastName){
_lastName = lastName;
}

public void setPhoneNumber(String phoneNumber){
_phoneNumber = phoneNumber;
}

public void fromByteArray(byte[] data) throws IOException{
ByteArrayInputStream bin = new ByteArrayInputStream(data);
DataInputStream din = new DataInputStream(bin);

_firstName = din.readUTF();
_lastName = din.readUTF();
_phoneNumber = din.readUTF();

din.close();
}

public byte[] toByteArray() throws IOException{
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);

dout.writeUTF(getFirstName());
dout.writeUTF(getLastName());
dout.writeUTF(getPhoneNumber());

dout.close();
return bout.toByteArray();
}
}

و این یکی هم کد برنامه ی اصلی هست :

import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

/**
*
* @author Hamed
*/
public class RmsDemo4 extends MIDlet implements CommandListener {

/** Creates a new instance of RmsDemo4 */
public RmsDemo4() {
initialize();
}

private Command itemCommand1;
private Form frmAdd;
private Command exitCommand1;
private Command cancelCommand1;
private Command itemCommand2;
private Form frmMain;
private Command backCommand1;
private TextField txtName;
private Spacer spacer1;
private TextField txtFamily;
private Spacer spacer2;
private TextField txtPhone;
private Alert alert1;
private Command itemCommand3;
private Form frmShow;
private Command backCommand2;



/** Called by the system to indicate that a command has been invoked on a particular displayable.
* @param command the Command that ws invoked
* @param displayable the Displayable on which the command was invoked
*/
public void commandAction(Command command, Displayable displayable) {
// Insert global pre-action code here
if (displayable == frmMain) {
if (command == itemCommand1) {
// Insert pre-action code here
getDisplay().setCurrent(get_frmAdd());
// Insert post-action code here
} else if (command == exitCommand1) {
// Insert pre-action code here
exitMIDlet();
// Insert post-action code here
} else if (command == itemCommand3) {
// Insert pre-action code here
getDisplay().setCurrent(get_frmShow());
// Insert post-action code here
RecordStore rs;
int recordID;
Contact contact = new Contact();
try{
rs = RecordStore.openRecordStore("MyRecord",false);
recordID = 1;
byte[] data = rs.getRecord(recordID);
contact.fromByteArray(data);
frmShow.append(contact.getLastName()+" - "+contact.getFirstName());
getDisplay().setCurrent(get_frmShow());
}
catch(RecordStoreException rx){
System.out.println(rx.toString());
}
catch(IOException ix){
System.out.println(ix.toString());
}
}
} else if (displayable == frmAdd) {
if (command == itemCommand2) {
// Insert pre-action code here
getDisplay().setCurrent(get_alert1(), get_frmAdd());
// Insert post-action code here
Contact contact = new Contact();
RecordStore rs;
contact.setFirstName(txtName.getString());
contact.setLastName(txtFamily.getString());
contact.setPhoneNumber(txtPhone.getString());

try{
rs = RecordStore.openRecordStore("MyRecord",true);
byte[] data = contact.toByteArray();
rs.addRecord(data,0,data.length);
getDisplay().setCurrent(alert1);
}
catch(RecordStoreException rx){

}
catch(IOException ix){

}
} else if (command == backCommand1) {
// Insert pre-action code here
getDisplay().setCurrent(get_frmMain());
// Insert post-action code here
}
} else if (displayable == frmShow) {
if (command == backCommand2) {
// Insert pre-action code here
getDisplay().setCurrent(get_frmMain());
// Insert post-action code here
}
}
// Insert global post-action code here
}

/** This method initializes UI of the application.
*/
private void initialize() {
// Insert pre-init code here
getDisplay().setCurrent(get_frmMain());
// Insert post-init code here
}

/**
* This method should return an instance of the display.
*/
public Display getDisplay() {
return Display.getDisplay(this);
}

/**
* This method should exit the midlet.
*/
public void exitMIDlet() {
getDisplay().setCurrent(null);
destroyApp(true);
notifyDestroyed();
}
/** This method returns instance for frmMain component and should be called instead of accessing frmMain field directly.
* @return Instance for frmMain component
*/
public Form get_frmMain() {
if (frmMain == null) {
// Insert pre-init code here
frmMain = new Form("Welcome to rms demo", new Item[0]);
frmMain.addCommand(get_itemCommand1());
frmMain.addCommand(get_exitCommand1());
frmMain.addCommand(get_itemCommand3());
frmMain.setCommandListener(this);
// Insert post-init code here
}
return frmMain;
}
/** This method returns instance for itemCommand1 component and should be called instead of accessing itemCommand1 field directly.
* @return Instance for itemCommand1 component
*/
public Command get_itemCommand1() {
if (itemCommand1 == null) {
// Insert pre-init code here
itemCommand1 = new Command("add contact", Command.ITEM, 1);
// Insert post-init code here
}
return itemCommand1;
}

/** This method returns instance for frmAdd component and should be called instead of accessing frmAdd field directly.
* @return Instance for frmAdd component
*/
public Form get_frmAdd() {
if (frmAdd == null) {
// Insert pre-init code here
frmAdd = new Form(null, new Item[] {
get_txtName(),
get_spacer1(),
get_txtFamily(),
get_spacer2(),
get_txtPhone()
});
frmAdd.addCommand(get_itemCommand2());
frmAdd.addCommand(get_backCommand1());
frmAdd.setCommandListener(this);
// Insert post-init code here
}
return frmAdd;
}

/** This method returns instance for exitCommand1 component and should be called instead of accessing exitCommand1 field directly.
* @return Instance for exitCommand1 component
*/
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;
}

/** This method returns instance for cancelCommand1 component and should be called instead of accessing cancelCommand1 field directly.
* @return Instance for cancelCommand1 component
*/
public Command get_cancelCommand1() {
if (cancelCommand1 == null) {
// Insert pre-init code here
cancelCommand1 = new Command("Cancel", Command.CANCEL, 1);
// Insert post-init code here
}
return cancelCommand1;
}

/** This method returns instance for itemCommand2 component and should be called instead of accessing itemCommand2 field directly.
* @return Instance for itemCommand2 component
*/
public Command get_itemCommand2() {
if (itemCommand2 == null) {
// Insert pre-init code here
itemCommand2 = new Command("Ok", Command.ITEM, 1);
// Insert post-init code here
}
return itemCommand2;
}

/** This method returns instance for backCommand1 component and should be called instead of accessing backCommand1 field directly.
* @return Instance for backCommand1 component
*/
public Command get_backCommand1() {
if (backCommand1 == null) {
// Insert pre-init code here
backCommand1 = new Command("Back", Command.BACK, 1);
// Insert post-init code here
}
return backCommand1;
}

/** This method returns instance for txtName component and should be called instead of accessing txtName field directly.
* @return Instance for txtName component
*/
public TextField get_txtName() {
if (txtName == null) {
// Insert pre-init code here
txtName = new TextField("Name:", null, 120, TextField.ANY);
// Insert post-init code here
}
return txtName;
}

/** This method returns instance for spacer1 component and should be called instead of accessing spacer1 field directly.
* @return Instance for spacer1 component
*/
public Spacer get_spacer1() {
if (spacer1 == null) {
// Insert pre-init code here
spacer1 = new Spacer(1000, 1);
// Insert post-init code here
}
return spacer1;
}

/** This method returns instance for txtFamily component and should be called instead of accessing txtFamily field directly.
* @return Instance for txtFamily component
*/
public TextField get_txtFamily() {
if (txtFamily == null) {
// Insert pre-init code here
txtFamily = new TextField("Family:", null, 120, TextField.ANY);
// Insert post-init code here
}
return txtFamily;
}

/** This method returns instance for spacer2 component and should be called instead of accessing spacer2 field directly.
* @return Instance for spacer2 component
*/
public Spacer get_spacer2() {
if (spacer2 == null) {
// Insert pre-init code here
spacer2 = new Spacer(1000, 1);
// Insert post-init code here
}
return spacer2;
}

/** This method returns instance for txtPhone component and should be called instead of accessing txtPhone field directly.
* @return Instance for txtPhone component
*/
public TextField get_txtPhone() {
if (txtPhone == null) {
// Insert pre-init code here
txtPhone = new TextField("PhoneNumber:", null, 120, TextField.ANY);
// Insert post-init code here
}
return txtPhone;
}

/** This method returns instance for alert1 component and should be called instead of accessing alert1 field directly.
* @return Instance for alert1 component
*/
public Alert get_alert1() {
if (alert1 == null) {
// Insert pre-init code here
alert1 = new Alert("Message Alert:", "Your record has been add.", null, AlertType.INFO);
alert1.setTimeout(2000);
// Insert post-init code here
}
return alert1;
}

/** This method returns instance for itemCommand3 component and should be called instead of accessing itemCommand3 field directly.
* @return Instance for itemCommand3 component
*/
public Command get_itemCommand3() {
if (itemCommand3 == null) {
// Insert pre-init code here
itemCommand3 = new Command("Show", Command.ITEM, 1);
// Insert post-init code here
}
return itemCommand3;
}

/** This method returns instance for frmShow component and should be called instead of accessing frmShow field directly.
* @return Instance for frmShow component
*/
public Form get_frmShow() {
if (frmShow == null) {
// Insert pre-init code here
frmShow = new Form("Record Show", new Item[0]);
frmShow.addCommand(get_backCommand2());
frmShow.setCommandListener(this);
// Insert post-init code here
}
return frmShow;
}

/** This method returns instance for backCommand2 component and should be called instead of accessing backCommand2 field directly.
* @return Instance for backCommand2 component
*/
public Command get_backCommand2() {
if (backCommand2 == null) {
// Insert pre-init code here
backCommand2 = new Command("Back", Command.BACK, 1);
// Insert post-init code here
}
return backCommand2;
}

public void startApp() {
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

}

ممنونم، در ضمن باید بگم که کلاس اول سه تا مقدار نام، نام خانوادگی و شماره تلفن رو در خودش ذخیره میکنه.