0

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!

2
  • 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. Commented Jul 10, 2013 at 1:04
  • It's not at run time, It's showing in my IDE when I compile. @user2357112 Commented Jul 10, 2013 at 1:23

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Peter is right. I'd look for AWT classes. java.awt.Point and Rect get me frequently when writing libraries or trying to use libraries for android.
I just realized the class which I am using is defined in the same package as the one which I am using it in. So is there a need to add the class path? @Peter
You need to make sure it's in the JAR file.
This actually looks more like the compiler is complaining about an unhandled checked exception, not an actual problem with the class not being found at runtime. I hope Sidd isn't going off to fix the wrong bug.
What do you mean by an unhandled checked exception?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.