0

I'm trying to build a library type-program in java, but I'm having some trouble with storing user input in my ArrayList. I have an ArrayList called 'list' that stores the title, author, publisher, genre, year and pages of the book and I need to allow users to input books to the ArrayList to later be able to view all the books stored in the ArrayList.

When I run the program it's throwing this error:

Exception in thread "main" java.lang.NullPointerException
    at thelibrary.addBooks(Book.java:73)
    at Menu.bookmenu(Menu.java:68)
    at Menu.main(Menu.java:27) 

Where am I going wrong?

    public static void addBooks() {
        Scanner newBooks = new Scanner(System.in);
        ArrayList<Book> list = null;

        for (int i = 0; i < 1; i++) {

System.out.println("\n\nAdd a new book to the library:");

        list.add(new Book(title, author, publisher, genre, year, pages));
        }

        System.out.println("You have successfully added a new book to the library!");
}
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Thomas Fritsch Nov 20 '17 at 21:50

1 Answers1

1

You declared list to be of type ArrayList, but you never called the ArrayList constructor (you set it equal to null, instead).

Instead of: ArrayList<Book> list = null;

Use ArrayList<Book> list = new ArrayList<Book>();.

Additionally, list dies when it leaves the scope of addBooks(). Try this, instead:

class thelibrary {
    public static ArrayList<Book> list = new ArrayList<Book>();

    public static void allBooks() {

            Book obj1 = new Book("Harry Potter and the Philosopher's Stone","JK Rowling","Bloomsbury","Genre",1997,223);
            Book obj2 = new Book("Alexander Hamilton","Ron Chernow","Head of Zeus","Biograophy",2016,818);
            Book obj3 = new Book("To Kill a Mockingbird","Harper Lee","Arrow","Southern Gothic",1960,309);

            list.add(obj1);
            list.add(obj2);
            list.add(obj3);

            for(Book ob : list) {

                System.out.println("Title: " + ob.title);
                System.out.println("Author: " + ob.author);
                System.out.println("Publisher: " + ob.publisher);
                System.out.println("Genre: " + ob.genre);
                System.out.println("Year: " + ob.year);
                System.out.println("Pages: " + ob.pages);

                System.out.println(" - - - - - - - - - - - - - - - - - - - - - - - - ");
            }

            Menu.bookmenu();
        }

        public static void addBooks() {
            Scanner newBooks = new Scanner(System.in);

            for (int i = 0; i < 1; i++) {
            System.out.println("\n\nAdd a new book to the library:");

            System.out.println("\n>Please enter book title: ");
            String title = newBooks.nextLine();

            System.out.println(">Please enter book author: ");
            String author = newBooks.nextLine();

            System.out.println(">Please enter book publisher: ");
            String publisher = newBooks.nextLine();

            System.out.println(">Please enter book genre: ");
            String genre = newBooks.nextLine();

            System.out.println(">Please enter book release year: ");
            int year = newBooks.nextInt();

            System.out.println(">Please enter number of book pages: ");
            int pages = newBooks.nextInt();

            list.add(new Book(title, author, publisher, genre, year, pages));

            System.out.println("You have successfully added a new book to the library!");
            Menu.bookmenu();   
        }
    }
Evan Weissburg
  • 1,564
  • 2
  • 17
  • 38
  • Thank you for your answer. I've changed this, but it still isn't storing any user input. Do you have any other ideas? – courtneyjones Nov 20 '17 at 21:40
  • 1
    Yes. In my menu's if-statement in order to redirect the user view what's stored in the ArrayList I've had to use 'thelibrary.addBook(null);' will this have something to do with it? I don't really know how else to call that part because I'm brand new to java! – courtneyjones Nov 20 '17 at 21:46
  • Oh -- there's another issue. Once you leave the scope of `addBooks`, the variable `list` stops existing. If you try printing the contents of `list` inside `addBooks`, it'll work. If you want to be able to output from somewhere else, see my updated answer (give me a couple minutes) – Evan Weissburg Nov 20 '17 at 21:48