Results 1 to 5 of 5
  1. #1
    Parashurama is offline Member
    Join Date
    Aug 2012
    Posts
    15
    Rep Power
    0

    Default 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:
    Java Code:
    public class Mob {
        // Variables
        String name;
        
    }
    My button code:
    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);
            }
        }
    I had to create a writeTemp method cause I could not add "throws IOException, ClassNotFoundException" to my Netbeans generated button code "header"

    writeTemp method:

    Java Code:
    public void writeTemp() throws IOException, ClassNotFoundException{
            FileOutputStream fos = new FileOutputStream("file.dat");
    	ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(mobTemp);
    	oos.close();
        }
    My button code fails if I have
    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

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default 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.

    ** This space for rent **

  3. #3
    Parashurama is offline Member
    Join Date
    Aug 2012
    Posts
    15
    Rep Power
    0

    Default 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.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default 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.

    ** This space for rent **

  5. #5
    Parashurama is offline Member
    Join Date
    Aug 2012
    Posts
    15
    Rep Power
    0

    Default Re: Object outputstream, cant write object with properties to file

    Yes, that was it.
    I never initialised the object, just declared it.

    Thanks for your help!

Similar Threads

  1. JSTL accessing properties on an object that extends ArrayList
    By chester_perry in forum Advanced Java
    Replies: 6
    Last Post: 07-29-2013, 05:54 PM
  2. Replies: 1
    Last Post: 02-02-2011, 06:11 PM
  3. Replies: 5
    Last Post: 05-17-2010, 01:13 PM
  4. Replies: 1
    Last Post: 01-29-2009, 09:35 AM
  5. Replies: 5
    Last Post: 01-20-2009, 09:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •