在Arraylist Java中存储用户输入

时间:2017-11-20 21:32:35

标签: java object arraylist user-input

我试图在java中构建一个库类型程序,但是我在将我的ArrayList中存储用户输入时遇到了一些麻烦。我有一个名为' list'存储书籍的titleauthorpublishergenreyearpages,我需要允许用户输入图书到ArrayList以后能够查看存储在ArrayList中的所有书籍。

当我运行该程序时,它会抛出此错误:

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) 

我哪里错了?

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

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

System.out.println(&#34; \ n \ n为图书馆添加新书:&#34;);

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

        System.out.println("You have successfully added a new book to the library!");
}

1 个答案:

答案 0 :(得分:1)

您声明list属于ArrayList类型,但您从未调用过ArrayList构造函数(您将其设置为等于null)。

而不是: ArrayList<Book> list = null;

使用ArrayList<Book> list = new ArrayList<Book>();

此外,list在离开addBooks()范围时会死亡。试试这个,而不是:

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();   
        }
    }