Results 1 to 9 of 9
  1. #1
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default ObjectInputStream issue

    I am trying to "import" an array saved by the ObjectOutputStream class. My problem is that my variable used to hold the array in the return statement is ignored. Its telling me that the variable has not been defined. I think this may be due to the try/catch statements, I don't know how they work though so I'm not sure.

    Here is my load() method:

    Java Code:
    public Thing[] load (){
    		
    		FileInputStream fis = null;
    		Thing[] loadData ;
    		
    		try {
    			fis = new FileInputStream("t.tmp");
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		ObjectInputStream ois;
    		try {
    			ois = new ObjectInputStream(fis);
    		
    			Thing [] loadData = (Thing[])ois.readObject();
    			ois.close();
    		
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    		return loadData;
    And if its any help here is my save method:

    Java Code:
    public void save(Thing[] saveDataArray){
    		
    		FileOutputStream fos = null;
    		try {
    			fos = new FileOutputStream("savedata.tmp");
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		try {
    			ObjectOutputStream oos = new ObjectOutputStream(fos);
    			
    			oos.writeObject(saveDataArray);
    			oos.close();
    			
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    Edit: I also just realized that I have to different file names there, oops :P
    Last edited by aianta; 04-13-2011 at 01:00 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Don't paraphrase error messages as your assumptions may be wrong. Please tell us the exact error. And initialize the variable at least with a null.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Java Code:
    Thing[] loadData;
    ....
    Thing [] loadData = (Thing[])ois.readObject();

  4. #4
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default

    Sorry about that, here is what Eclipse is telling me wont work:

    Java Code:
    return loadData;
    loadData can not be resolved to a variable

  5. #5
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default

    Ok, I initialized the variable to null, and now I get this error on the following line:

    duplicate local variable

    Java Code:
    Thing [] loadData = (Thing[])ois.readObject();
    Which makes sense but, when I remove the initial declaration:

    Java Code:
    Thing[] loadData = null;
    It tells me:

    Java Code:
    return loadData;
    loadData can not be resolved to a variable

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Because removing the initial declaration was wrong.

  7. #7
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default

    So, then whats the right thing to do?

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Don't declare the variable twice, obviously. How would you correct the below code?
    Java Code:
    int number;
    int number = 10;

  9. #9
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default

    (facepalm) Sorry for wasting your time. wow, I feel stupid.

Similar Threads

  1. ObjectInputStream() for Linux and Windows
    By eh1476 in forum Threads and Synchronization
    Replies: 0
    Last Post: 03-04-2011, 09:52 PM
  2. ObjectInputStream wait for readable objects
    By Singing Boyo in forum New To Java
    Replies: 2
    Last Post: 06-08-2009, 03:43 AM
  3. ObjectInputStream does not initialize
    By Singing Boyo in forum New To Java
    Replies: 1
    Last Post: 06-03-2009, 08:11 AM
  4. Reading from ObjectInputStream
    By deepthought015 in forum Networking
    Replies: 8
    Last Post: 04-28-2009, 05:57 PM
  5. Stops when opening ObjectInputStream
    By Norberhuis in forum Networking
    Replies: 4
    Last Post: 01-09-2009, 04:19 PM

Posting Permissions

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