Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By Tolls
  • 1 Post By Tolls

Thread: Java save to file / open from file - trouble

  1. #1
    joelBeaton is offline Member
    Join Date
    Mar 2012
    Location
    England
    Posts
    13
    Rep Power
    0

    Default Java save to file / open from file - trouble

    Hi all, Despite being relatively new to all this I think this is the correct place to post this. I have a written application that functions and in and of itself it works well (well, mostly). I have written it so that it when it opens, if there is not *.data file in the main directory then a fresh new copy of the program will open. But If there if the program has been run previously and the state saved well then next time you open it everything is there.

    The problem arises when I try and open a previously saved file. I get the following errors and stack trace:

    run:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at recruitSystemCore.RecruitmentTrackingCore.getListO fActiveNames(RecruitmentTrackingCore.java:39)
    at recruitGUI.RecruitmentGUI.listAllActiveProjectPrep ares(RecruitmentGUI.java:72)
    at recruitGUI.RecruitmentGUI.<init>(RecruitmentGUI.ja va:45)
    at recruitGUI.RecruitmentGUI$25.run(RecruitmentGUI.ja va:1137)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 597)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    BUILD SUCCESSFUL (total time: 7 seconds)


    The structure of the program is all SystemCore type processes in one package which is controlled by RecruitmentTrackingCore and a GUI in a separate package and all messages from the GUI are passed through to the RecruitmentTrackingCore class.

    The file save method is located in the RecruitmentTrackingCore class. The code for the save() method as written is:

    /**
    * Saves the state of the receiver to the file Recruit.data.
    */

    public void save()
    {
    try
    {
    FileOutputStream fos = new FileOutputStream("Recruit.data");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(this);
    oos.flush();
    oos.close();
    fos.close();
    }
    catch (Exception ex)
    {
    System.out.println("Problem storing state of Recruit System");
    System.exit(1);
    }
    }

    And my file open method looks like this:

    /**
    * Creates and returns a new RecruitmentTrackingCore object.
    * Reads in the state of the object from the file Recruit.data
    * if there is no such file, returns the object in its initial state.
    *
    * @return a new RecruitmentTrackingCore object
    */

    public static RecruitmentTrackingCore getRecruitmentTrackingCoreObject()
    {
    RecruitmentTrackingCore recruit = null;
    try
    {
    FileInputStream fis = new FileInputStream("Recruit.data");
    ObjectInputStream ois = new ObjectInputStream(fis);
    recruit = (RecruitmentTrackingCore) ois.readObject();
    ois.close();
    //fis.close();
    }
    catch (FileNotFoundException ex)
    {
    recruit = new RecruitmentTrackingCore();
    }
    catch (Exception ex)
    {
    System.out.println(
    "Initialisation error. You may need to delete the data file. ");
    System.exit(1);
    }
    return recruit;
    }

    What am I doing wrong here? Anyone? I have "googled" till my eyes have almost fallen out of my head and cant figure it out! Many thanks.

    Joel

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,796
    Rep Power
    19

    Default Re: Java save to file / open from file - trouble

    What is null on line 39 then?
    You ought to log the exceptions there as well, IMO.
    printStackTrace().
    I wouldn't be surprised if you were getting a FileNotFound and not realising it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    joelBeaton is offline Member
    Join Date
    Mar 2012
    Location
    England
    Posts
    13
    Rep Power
    0

    Default Re: Java save to file / open from file - trouble

    Thank for your answer.

    The file is being created ("recruit.data") and I see it. The class RecruitmentTrackingCore contains three collections - recruiterSet and setOfPublic. Its these that are null. I have been assuming that they are populated because - well I obviously typed something in them and then clicked save and then saw the file appear. When the GUI opens it tries to populate a few jLists and from these collections. When I put try catch statements there, the program opens and prints out that these are null.

    So the question becomes - how come these collections save as null? If you could have a look at my code for this class? Included full class file below.

    Java Code:
    package recruitSystemCore;
    
    import java.util.*;
    import java.io.*;
    import java.io.Serializable;
    import java.util.SortedSet;
    import java.util.TreeSet;
    
    /**
     *
     * @author Joel
     */
    public class RecruitmentTrackingCore implements Serializable
    {
    
        private static SortedSet<ProjectPrepare> setOfPublic;
        private static SortedSet<ProjectPrepare> deletedPublic;
        private static SortedSet<Recruiter> recruiterSet;
    
        /**
         * Constructor. Takes no arguments. Generates a sorted set of
         * ProjectPrepare Objects and handles these.
         */
        public RecruitmentTrackingCore()
        {
            setOfPublic = new TreeSet<ProjectPrepare>();
            deletedPublic = new TreeSet<ProjectPrepare>();
            recruiterSet = new TreeSet<Recruiter>();
        }
    Last edited by joelBeaton; 03-23-2012 at 12:48 PM.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,796
    Rep Power
    19

    Default Re: Java save to file / open from file - trouble

    static class fields are not serialised.
    Serialisation applies to instances of a class, not to the class itself.

    ETA: In fact, why are they static?
    You seem to be using them as instance attributes (at least looking at the constructor, I'm not looking at anymore unformatted code than that).

    Please use [code] tags [/code] when posting code.
    joelBeaton likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    joelBeaton is offline Member
    Join Date
    Mar 2012
    Location
    England
    Posts
    13
    Rep Power
    0

    Default Re: Java save to file / open from file - trouble

    Oh! sorry. will use the code /code thing. Wasn't sure how I could make it look properly formatted.

    Didnt know that on static and serialization. I changed that and it opens the last saved state!! So thank you!

    I assigned these as static because I will only ever want one copy of these, ever. No matter how many people open it and save it etc I don't want to have "different versions" ever. So my solution for that was to have these as static. I am in progress on adding this to a server / client environment and will add synchronization and threading and thought that having these as static would forward my cause of "no matter how many are created they will always be the same".

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,796
    Rep Power
    19

    Default Re: Java save to file / open from file - trouble

    If you only ever want one, then you could look at making it a Singleton.
    You should be able to find a suitable example if you Google - Java Singleton.
    joelBeaton likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    joelBeaton is offline Member
    Join Date
    Mar 2012
    Location
    England
    Posts
    13
    Rep Power
    0

    Default Re: Java save to file / open from file - trouble

    Thanks for your help!

Similar Threads

  1. Replies: 3
    Last Post: 06-21-2011, 04:51 PM
  2. How to track Save And open a Junit test Java file?
    By jaydip.biswas in forum Eclipse
    Replies: 0
    Last Post: 03-15-2010, 06:44 AM
  3. Replies: 0
    Last Post: 03-05-2010, 04:21 AM
  4. How to open a SAVE file dialog box without using SWING or AWT
    By SAYAN in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 02-25-2010, 01:30 AM
  5. To open an image file such as Jpeg file using JAva Program
    By itmani2020 in forum Advanced Java
    Replies: 10
    Last Post: 07-11-2008, 09:57 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
  •