2

I just made my first I/O based stuff in Java. I want to check if the content written to a file is properly saved in, or not. For which i wrote the following code..

    import java.io.*;

    public class check implements Serializable {
        //Make two variables and methods to initialise them
        private int height;
        private int width;
        public void setWidth(int w){
            width = w;
        }
        public void setHeight(int h){
            height = h;
        }
        public static void main(String[] args) {

        check obj = new check();
        obj.setHeight(20);
        obj.setWidth(30);

    try{
        FileOutputStream fs = new FileOutputStream("foo.txt");
        ObjectOutputStream os = new ObjectOutputStream(fs);
        os.writeObject(obj);
        os.close();
    }
    catch(IOException ex){
    }
    //We set them to null so we can't access the objects on heap.
      obj = null;  

    //Now we read them back from file   
    try{
        ObjectInputStream is = new ObjectInputStream(new FileInputStream("foo.txt")); 
        check stored = (check) is.readObject();

    //Check to see if it worked.
     System.out.println("Variable, stored contains.." + stored.getType());
    }
    catch(IOException ex){
        }

    }
 }

But it produces the following error.

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown
    at check.Check.main(Check.java:33)

Anyone got any idea to solve the issue?

1
  • 1
    Don't try to run code while you still have compilation errors. Open the "Problems" view of Eclipse, and fix all the compilation errors listed there before trying to run your code. What you're doing is like trying to drive a car which doesn't have wheels yet. Commented Nov 15, 2013 at 7:46

3 Answers 3

1

Take a look at http://docs.oracle.com/javase/7/docs/api/java/io/ObjectInputStream.html#readObject(). The method lists a couple of exceptions. For every exception listed that is not a sub-class of RuntimeException you need to either catch the exception or declare that the method can throw that exception. You have only done this for IOException. You also need to do this for the other exceptions listed in the documentation. This needs to be done for all methods that throw non-runtime exceptions.

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

3 Comments

That's not really the issue.. :(
@JuzzCoding Then I don't really know what can be done in that case. I was going by what your error message says. I cannot spot anything that causes that error message. Maybe you need to provide more details here.
nvm, it's resolved now.. it actually couldn't find the definition of function getType()
0

Your IDE is letting you run some code even though you are missing some classes or despite having compilation errors. Fix the compilation errors before you run them.

4 Comments

I'm unable to fix the compilation errors.. any help regarding that?
@JuzzCoding Go to Window --> Output --> Output. You should see the errors and fix them or press Ctrl + 4.
I can go directly to Window->Output (i.e, Ctrl+4 ), but that makes no change in the error message, the one i posted.. :\
@JuzzCoding Having that window open, do Run --> Clean and Build Project. You will see compilation errors. The one you posted is a run-time error.
0

Your code is uncompilable at the moment. Line 36 fails.

System.out.println("Variable, stored contains.." + stored.getType());

This is because the class check does not contain a method getType(). Maybe You meant something along the lines of getClass().getName()?

Fix this error and try again. Your own error message does not make sense to me - is it generated by an IDE?

PS. Have a look at Java coding conventions regarding the naming of classes, variables and such. :)

Comments

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.