Results 1 to 9 of 9
Thread: ObjectInputStream issue
- 04-13-2011, 12:42 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 60
- Rep Power
- 0
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:
And if its any help here is my save 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;
Edit: I also just realized that I have to different file names there, oops :PJava 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(); }Last edited by aianta; 04-13-2011 at 01:00 AM.
-
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.
- 04-13-2011, 12:52 AM #3
Java Code:Thing[] loadData; .... Thing [] loadData = (Thing[])ois.readObject();
- 04-13-2011, 12:57 AM #4
Member
- Join Date
- Apr 2011
- Posts
- 60
- Rep Power
- 0
Sorry about that, here is what Eclipse is telling me wont work:
loadData can not be resolved to a variableJava Code:return loadData;
- 04-13-2011, 01:03 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 60
- Rep Power
- 0
Ok, I initialized the variable to null, and now I get this error on the following line:
duplicate local variable
Which makes sense but, when I remove the initial declaration:Java Code:Thing [] loadData = (Thing[])ois.readObject();
It tells me:Java Code:Thing[] loadData = null;
loadData can not be resolved to a variableJava Code:return loadData;
- 04-13-2011, 01:06 AM #6
Because removing the initial declaration was wrong.
- 04-13-2011, 01:10 AM #7
Member
- Join Date
- Apr 2011
- Posts
- 60
- Rep Power
- 0
So, then whats the right thing to do?
- 04-13-2011, 01:16 AM #8
Don't declare the variable twice, obviously. How would you correct the below code?
Java Code:int number; int number = 10;
- 04-13-2011, 01:22 AM #9
Member
- Join Date
- Apr 2011
- Posts
- 60
- Rep Power
- 0
Similar Threads
-
ObjectInputStream() for Linux and Windows
By eh1476 in forum Threads and SynchronizationReplies: 0Last Post: 03-04-2011, 09:52 PM -
ObjectInputStream wait for readable objects
By Singing Boyo in forum New To JavaReplies: 2Last Post: 06-08-2009, 03:43 AM -
ObjectInputStream does not initialize
By Singing Boyo in forum New To JavaReplies: 1Last Post: 06-03-2009, 08:11 AM -
Reading from ObjectInputStream
By deepthought015 in forum NetworkingReplies: 8Last Post: 04-28-2009, 05:57 PM -
Stops when opening ObjectInputStream
By Norberhuis in forum NetworkingReplies: 4Last Post: 01-09-2009, 04:19 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks