-1

I want to read from a file using FileInputStream and ObjectInputStream. I have created BankAccount class which implements Externalizable interface and @overriden both its methods. I do not understand why it throws an IOException.

These are the overriden methods of interface Externalizable:

@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    char a = in.readChar();
    id = in.read();
    username = (String)in.readObject();
    name = (String)in.readObject();
    password = (String)in.readObject();
    amount = in.readDouble();
}

@Override
public void writeExternal(ObjectOutput out) throws IOException {
    out.writeChar('A');
    out.write(id);
    out.writeObject(username);
    out.writeObject(name);
    out.writeObject(password);
    out.writeDouble(0);
}

This is the code where i call these methods in main class:

try
{    
    // Writing the object into file 
    FileOutputStream file = new FileOutputStream(path); 
    ObjectOutputStream out = new ObjectOutputStream(file); 

    // Method for serialization of object 
    out.writeObject(account);

    out.close();
    file.close();
} 
catch(IOException ex) 
{ 
    System.out.println("IOException is caught"); 
} 

try {
    //Reading object from file
    FileInputStream file = new FileInputStream(path); 
    ObjectInputStream out = new ObjectInputStream(file); 


    BankAccount bankk = (BankAccount)out.readObject();
    System.out.println(bankk);

    out.close();
    file.close();
} 
catch(IOException ex) 
{ 
    System.out.println("IOException is caught"); 
}

This is what it threw:

java.io.InvalidClassException: al.tct.bank_project.AdminAccount; no valid constructor
at java.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(ObjectStreamClass.java:157)
at java.io.ObjectStreamClass.checkDeserialize(ObjectStreamClass.java:862)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2038)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1568)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:428)
at al.tct.bank_project.BankApp$RegisterStage$1.writeToFile(BankApp.java:463)
at al.tct.bank_project.BankApp$RegisterStage$1.handle(BankApp.java:412)
at al.tct.bank_project.BankApp$RegisterStage$1.handle(BankApp.java:404)
user207421
  • 305,947
  • 44
  • 307
  • 483
Amel B
  • 25
  • 9

1 Answers1

0

Stack Traces contain at least 4 interesting bits of information:

[1] The type. IOException has subclasses. Perhaps it is a FileNotFoundException.

[2] the stack trace. This is a line pointing at where in your source file the error occurs, and the 'call chain' that led to this method being executed.

[3] the message. Exceptions can have a message explaining more in readable english.

[4] the causal chain. Exceptions can have a cause, and usually that exception is itself interesting (and that cause can have a cause).

There's even more (such as the suppress chain).

If you ever catch an exception and attempt to print it, you're messing up, printing ALL THIS is hard to do and yet it's all important.

So, do NOT catch an exception, unless you can actually handle it. printing an error message or logging it is not handling it.

Here, just make your method(s) be declared as 'throws IOException'. If that is not possible (can occur only if you are extending/implementing methods from superclasses/interfaces which don't let you do that), then the 'I dont know what to do about it' code is this:

catch (ExceptionICannotHandle e) {
    throw new RuntimeException(e);
}

that is the only way to guarantee you get allll that info.

I suggest you update your code, and then the cause of this error will become clear to you.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72