Results 1 to 5 of 5
- 09-06-2012, 11:58 AM #1
Member
- Join Date
- Aug 2012
- Posts
- 15
- Rep Power
- 0
Object outputstream, cant write object with properties to file
Hi,
I searched through the forum, but could not find an answer to my question so Ill ask in this post.
What I want to do is writing an object to a file.
My issue is that It only works if the Object is "empty"
I have a class called Mob, which have a name property (String).
If I try to write the object to file without setting the name property I get a file and no errors, but if I set the name property I get loads of errors.
See below for erroes and code:
Code :
Mob Class:
My button code:Java Code:public class Mob { // Variables String name; }
I had to create a writeTemp method cause I could not add "throws IOException, ClassNotFoundException" to my Netbeans generated button code "header"Java Code:private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) { mobTemp.name = txtMobName.getText(); try { // write mobTemp to a file writeTemp(); } catch (IOException ex) { Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex); } catch (ClassNotFoundException ex) { Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex); } }
writeTemp method:
My button code fails if I haveJava Code:public void writeTemp() throws IOException, ClassNotFoundException{ FileOutputStream fos = new FileOutputStream("file.dat"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(mobTemp); oos.close(); }
mobTemp.name = txtMobName.getText();
but does not fail if I remove it. Then it makes the file.dat file.
I hope this was understandable. Please let me know if more info is needed.
Parashurama
- 09-06-2012, 12:10 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Object outputstream, cant write object with properties to file
Without the full exception (plus stacktrace) we can't say.
Please do not ask for code as refusal often offends.
- 09-06-2012, 12:41 PM #3
Member
- Join Date
- Aug 2012
- Posts
- 15
- Rep Power
- 0
Re: Object outputstream, cant write object with properties to file
Sorry, I ment to write down the errors as well.
Here they are:
Java Code:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at MUD.MainWindow.btnSaveActionPerformed(MainWindow.java:249) "This corresponds to line 2 in my button code" at MUD.MainWindow.access$000(MainWindow.java:18) at MUD.MainWindow$1.actionPerformed(MainWindow.java:166) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6505) at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) at java.awt.Component.processEvent(Component.java:6270) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4861) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) at java.awt.Container.dispatchEventImpl(Container.java:2273) at java.awt.Window.dispatchEventImpl(Window.java:2719) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:703) at java.awt.EventQueue.access$000(EventQueue.java:102) at java.awt.EventQueue$3.run(EventQueue.java:662) at java.awt.EventQueue$3.run(EventQueue.java:660) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue$4.run(EventQueue.java:676) at java.awt.EventQueue$4.run(EventQueue.java:674) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:673) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139) at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
Last edited by Parashurama; 09-06-2012 at 12:44 PM.
- 09-06-2012, 02:09 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Object outputstream, cant write object with properties to file
txtMobName is null.
As to why (other than it not being initialised) I can't say.Please do not ask for code as refusal often offends.
- 09-06-2012, 02:48 PM #5
Member
- Join Date
- Aug 2012
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
Insert class file as object in a table & read the object from the blob.
By facemeguru in forum New To JavaReplies: 1Last Post: 02-02-2011, 06:11 PM -
JSTL accessing properties on an object that extends ArrayList
By chester_perry in forum Advanced JavaReplies: 4Last Post: 01-13-2011, 10:07 AM -
Finding an object of given properties in an ArrayList
By sonny in forum New To JavaReplies: 5Last Post: 05-17-2010, 01:13 PM -
How to write a java bean class object to XML file
By Java Tip in forum java.ioReplies: 1Last Post: 01-29-2009, 09:35 AM -
How to read from and write to .properties file from a jsp
By MAGNUM in forum New To JavaReplies: 5Last Post: 01-20-2009, 09:08 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks