Reading objects from a file
I've been trying to read an object. I've listed the code for the Firearms Class and the code i'm using to read the object.
// holds detailed information about each gun in the system.
public class Firearms implements Serializable{
// declaration of variables.
String manf;
String model;
String sn;
String action;
String cal;
String barrel;
//Constructor
public Firearms() {
manf = null;
model = null;
sn = null;
cal = null;
action = null;
barrel = null;
}
//puts values into each variable
public Firearms(String inManf, String inModel, String inSN, String inAct, String inCal, String inBar ){
manf = inManf;
model = inModel;
sn = inSN;
action = inAct;
cal = inCal;
barrel = inBar;
}
private void jButton6MouseClicked(java.awt.event.MouseEvent evt) {
//vector to hold all the firearms once returned
Vector v = new Vector();
Firearms aGun;
try{
FileInputStream fis= new FileInputStream("C:\\Guns.tmp");
BufferedInputStream bis = new BufferedInputStream(fis);
ObjectInputStream ois = new ObjectInputStream(bis);
aGun = (Firearms)ois.readObject();
v.addElement(aGun);
jComboBox1.addItem(new String(aGun.getManf() + " " + aGun.getModel() + "-" + aGun.getSn()));
ois.close();
}
catch(Exception e){
System.out.println("Can't Read Gun");
}
ChooseFirearm_Internal.setVisible(true);
}
One other question is whats the best way to loop through the file. can i check to see if it equals null or is there a method that can take care of this.
thanks
[1732 byte] By [
ddecreea] at [2007-11-27 9:02:02]

You didn't ask a question (for the first part) or tell us the error.Message was edited by: jbish
jbisha at 2007-7-12 21:32:13 >

lol sorry bout that. Got distracted while posting. The program is making all of the streams correctly. It won't do the:aGun = (Firearms)ois.readObject();it catches an exception as it tries to execute this line of code. any ideas?
Print the stack trace of the exception when you catch it, it gives lots of good infoe.printStackTrace();
its not returning anything with that line of code. I'm still not getting the object to be read.
> its not returning anything with that line of code.
> I'm still not getting the object to be read.
Nothing printed when you made this change?
catch(Exception e){
System.out.println("Can't Read Gun");
e.printStackTrace();
}
just ran it a 2nd time and this time it did.
java.io.InvalidClassException: shootingjournal.Firearms; local class incompatible: stream classdesc serialVersionUID = 8696766358271959352, local class serialVersionUID = -4496115183548152168
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:562)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1583)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1496)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1732)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at shootingjournal.JournalPage.jButton6MouseClicked(JournalPage.java:1284)
at shootingjournal.JournalPage.access$400(JournalPage.java:16)
at shootingjournal.JournalPage$9.mouseClicked(JournalPage.java:478)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253)
at java.awt.Component.processMouseEvent(Component.java:6041)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3995)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
does this mean i can't use a user defined object with readObject()?
> does this mean i can't use a user defined object with
> readObject()?
You can, but you have to make sure that the version of the object that you store is the same version that you're using when you retrieve it. I'm not up on the details, but you can read about it here, under Version Control:
http://java.sun.com/developer/technicalArticles/Programming/serialization/
alright let me retry creating the Guns.tmp file
thats what fixed it. thanks alot for your help. must have changed the variable's order by mistake. thanks
No problem. If you give the class a SerialVersionUID, and change it each time you make a change to the class, you can easily tell which version of the class the stored object wants when you get that error.
alright i'll throw that in the Score Sheet when i do that one. thanks again
One More Question:
How do i loop through the whole file. i tried while(ois.available() != 0) but that doesn't read anything. the class i'm using has 5 properties. (aGun = ois.readObject) != null) will not work. i get the following error
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2552)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1297)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at shootingjournal.JournalPage.jButton6MouseClicked(JournalPage.java:1295)
at shootingjournal.JournalPage.access$400(JournalPage.java:16)
at shootingjournal.JournalPage$9.mouseClicked(JournalPage.java:481)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253)
at java.awt.Component.processMouseEvent(Component.java:6041)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3995)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
any suggestions?
Any answers as to how to loop through the whole file