-
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:
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:
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
-
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.
-
Code:
Thing[] loadData;
....
Thing [] loadData = (Thing[])ois.readObject();
-
Sorry about that, here is what Eclipse is telling me wont work:
loadData can not be resolved to a variable
-
Ok, I initialized the variable to null, and now I get this error on the following line:
duplicate local variable
Code:
Thing [] loadData = (Thing[])ois.readObject();
Which makes sense but, when I remove the initial declaration:
Code:
Thing[] loadData = null;
It tells me:
loadData can not be resolved to a variable
-
Because removing the initial declaration was wrong.
-
So, then whats the right thing to do?
-
Don't declare the variable twice, obviously. How would you correct the below code?
Code:
int number;
int number = 10;
-
(facepalm) Sorry for wasting your time. wow, I feel stupid.