Results 1 to 2 of 2
  1. #1
    mckeene is offline Member
    Join Date
    Apr 2015
    Posts
    1
    Rep Power
    0

    Default Try-Catch - java.lang.NullPointerException

    Hello guys

    I am about to finish a school project for introduction to Java and only have a single problem left in my program.
    I can not get the Try-Catch to work. I am trying to add an array list of a book with four different values. The book.dat file is created but contains this "
    Java Code:
    srjava.util.ArrayListxœaIsizexpwxsrjava.util.ArrayListxœaIsizexpwx
    " when I open it in a text editor.
    Below is my try-catch block.

    Java Code:
    // Write books to file
    	public void writeBooksToFile() {
    		
    		File file = new File("books.dat");
    		
    		// if(booksDatabase != null && !booksDatabase.isEmpty()) {
    			try (ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(file, true))) {
    				output.writeObject(Books);
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	//}
    
    	// Read books from file
    	@SuppressWarnings("unchecked")
    	public void readBooksFromFile() {
    		// if(booksDatabase != null && !booksDatabase.isEmpty()) {
    			try (ObjectInputStream input = new ObjectInputStream(new FileInputStream("books.dat"))) {
    					try {
    						Books = (List<Book>) (input.readObject());
    						addBook(null);
    						System.out.print(Books);
    						
    						
    					} catch (ClassNotFoundException e) {
    						e.printStackTrace();
    					}
    			
    			} catch (IOException e) {
    				e.printStackTrace();				
    			}
    		// }
    		//else {
    			//System.out.println("No book database found");
    		//}
    	}
    Can anyone see what I am doing wrong? If you need additional code please let me know

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Try-Catch - java.lang.NullPointerException

    Quote Originally Posted by mckeene View Post
    The book.dat file is created but contains this "
    Java Code:
    srjava.util.ArrayListxœaIsizexpwxsrjava.util.ArrayListxœaIsizexpwx
    " when I open it in a text editor.
    Nope. The header wouldn't be repeated halfway through the file. Looks like you pasted the whole thing twice here.

    Does the List load from file correctly, with all content intact?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. java.lang.NullPointerException
    By javastarter1 in forum New To Java
    Replies: 4
    Last Post: 08-04-2013, 12:51 AM
  2. java.lang.NullPointerException
    By Pombi in forum New To Java
    Replies: 6
    Last Post: 05-15-2010, 03:12 PM
  3. java.lang.nullPointerException
    By KSUliz in forum New To Java
    Replies: 10
    Last Post: 04-11-2010, 07:15 PM
  4. java.lang.NullPointerException
    By Travis in forum AWT / Swing
    Replies: 7
    Last Post: 09-18-2009, 05:49 PM
  5. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 2
    Last Post: 02-27-2009, 10:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •