0

I'm trying to read an ArrayList of objects from a BinaryI/O but I keep getting the error "class java.lang.Boolean cannot be cast to class java.util.ArrayList (java.lang.Boolean and java.util.ArrayList are in module java.base of loader 'bootstrap')"

This is the code

public void readAdmin(){
     try{
         File file = new File("Administrators.dat");
         file.createNewFile();
         ObjectInputStream input = new ObjectInputStream(new FileInputStream("Administrators.dat"));
         ArrayList<Administrator> adms = (ArrayList<Administrator>) input.readObject(); 
         usercontroller.setAdmins(adms);

     }
     catch(IOException e){
         System.out.println("Input of administrator data error.");
     }
     catch (ClassNotFoundException cn){
         System.out.println("Administrator class not existent.");
     }

The line "ArrayList adms = (ArrayList) input.readObject();" seems to be the one causing the error when I call the "readAdmin" method in main.

Any ideas on how I can fix this?

2
  • Looks like Administrators.dat doesn't contain what you expect it to. Commented Jan 29, 2023 at 3:32
  • Answer in link below might solve your issue. Input.readObject is most likely only returning Administrator instead of list of Administrator. stackoverflow.com/questions/62843415/… Commented Jan 29, 2023 at 4:25

0

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.