Java - compilation error debug -
i have compilation error can't seem fix. gives me message type mismatch: cannot convert object list. below have added methods says error in.
// read books file public void readbooksfromfile() { if(booksdatabase != null && !booksdatabase.isempty()) { try (objectinputstream input = new objectinputstream(new fileinputstream(booksdatabase))) { books = input.readobject(); // error here } catch (ioexception e) { e.printstacktrace(); } } }
the second method
public listbook(string booksdatabase) { this.booksdatabase = booksdatabase; readbooksfromfile(); // error here }
the third method
public static void main (string[] args){ // creates listbook object keep track of books in library listbook lb = new listbook("books.dat"); // error here // add books listbook object lb.addbook(new book("e-commerce", "kenneth laudon", "2014", "024449")); lb.addbook(new book("java programming", "daniel liang", "2014", "761312")); lb.addbook(new book("eu internet law", "andrej savin", "2015", "429379")); //new object //getter setter methods librarianmenu li = new librarianmenu(lb); //execute function librarian.java }
how fix error?
objectinputstream returns object; you're assigning books, of type list. bad idea.
worse, think you're imagining readobject can iterate on collection , read multiple books list. think should write method reads single book , iterate on collection, adding individual books list.