0
votes

In previous code in my program, I had saved an ArrayList (consisting of objects of a custom class called location as you can see in my code) in a file using ObjectOutputStream and FileOutPutStream. However, when trying to retrieve the object from the file, using ObjectInputStream, I am getting an error saying that I have an unhandled exception (ClassNotFoundException).

Here's the code I used to get the ArrayList out of the file:

String file = "file";

ObjectInputStream input = new ObjectInputStream(new FileInputStream("file"));



ArrayList<location> arrayList = new ArrayList<location>();
arrayList = (ArrayList) input.readObject();

The error is on the line where I call the .readObject() method. Any Help will be appreciated as I am new to Java. Thank You!

1
Are you getting an exception at runtime, with a stack trace and everything, or are you getting red squigglies in your IDE? If it's the second thing, then you need to use a try-catch block to handle the exception. - user2357112 supports Monica
It's not at run time, It's showing in my IDE when I compile. @user2357112 - Sidd Menon

1 Answers

1
votes

That means the class you sent could not be found in your app. You have to add it to the class path of the app, or only send classes the app has. In your case, the missing class will be in the ArrayList as ArrayList will always be there.

Nothing mysterious is going on, the error means just what it says.

It would be more useful if the exception told you which class was missing. I think Java 7 does this now.