1

I get a java.lang.ClassCastException while reading a binary file with a generic method. I was trying to read two diferent files with the same method but then I get the exception and I dont know why.

This is the code in the method generic for read the file: Read is a method part of the Class ReadBenary.

 import java.io.ObjectInputStream;
 import java.io.FileInputStream;
 import java.io.IOException;

 public class ReadBinary{

private ObjectInputStream reader;

public boolean open(String name) {
    try {
        reader= new ObjectInputStream(new FileInputStream(name));
        return true;
    } catch (IOException exception) {
        System.err.println("Error");
        return false;
    }
}

public void close() {
    try {
        if (reader != null) {
            lector.close();
        }
    } catch (IOException exception) {
        System.err.println("Error");
    }
}

public <E> E read() {

    E object = null;
    try {
        object = (E) reader.readObject();
    } catch (ClassNotFoundException ex) {
        System.err.println("Class dont found");
        System.err.println(ex);
    } catch (IOException ex) {
        System.err.println("End of the file");
        System.err.println(ex);
    } catch (ClassCastException ex) {
        System.err.println("Class dont found");
    }
    return object;
}

This is the code in the main:

 ReadBinary reader= new ReadBinary();

      if (reader.open(file3)) {
      *  Bill bill = reader.read();
        while (bill != null) {
            manejadorBill.insert(bill);
            bill = reader.read();
        }
        reader.close();
       }

    if (reader.open(file1)) {
        Customer customer= reader.read();
        while (customer != null) {
            manejadorCustomer.insert(customer);
            customer = reader.read();
        }
        reader.close();
    }

This is the method that write the objects in the file:

public <E> void escribir(E objeto) {
    try {
        escritor.writeObject(objeto);
    } catch (IOException exception) {
        System.err.print("Error rawr");
    }
}

Bill and Customer are two independent class. When i try to run the program it says that Customer cant be cast as Bill.

It says that the error occurs in the line with *.

3
  • What is ReadBinary? How is it reading the file? How is a Bill represented in the file? Commented Dec 4, 2013 at 21:26
  • You need to tell us what those classes are, and their place in a hierarchy. Commented Dec 4, 2013 at 21:27
  • You need to post the exception. It tells you exactly what the class of the object really was. Commented Dec 4, 2013 at 23:12

2 Answers 2

1

It seems that you don't have Bill objects in file3. To check this change read() to:

public <E> E read() {
    Objject obj;
    E object = null;
    try {
          obj = reader.readObject();
          System.out.println(obj.getClass().getName())       
          object = (E) obj;
    } catch (ClassNotFoundException ex) {
        System.err.println("Class dont found");
        System.err.println(ex);
    } catch (IOException ex) {
        System.err.println("End of the file");
        System.err.println(ex);
    } catch (ClassCastException ex) {
        System.err.println("Class dont found");
    }
    return object;
}

to see what are actual types.

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

Comments

0

The ClassCastException means that the next object written to the stream was not in fact a Bill. Check the code that writes the objects, to see what types of objects it is in fact writing. Some System.out.printlns might be helpful to see exactly what is being written.

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.