简体   繁体   English

在Arraylist Java中存储用户输入

[英]Storing User Input in Arraylist Java

I'm trying to build a library type-program in java, but I'm having some trouble with storing user input in my ArrayList. 我试图用Java构建一个库类型程序,但是在将用户输入存储在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. 我有一个名为“ list ”的ArrayList,用于存储publishertitleauthorpublishergenreyearpages ,我需要允许用户向ArrayList输入书籍,以便以后能够查看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:"); System.out.println(“ \\ n \\ n向图书馆添加新书:”);

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

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

You declared list to be of type ArrayList , but you never called the ArrayList constructor (you set it equal to null , instead). 您声明list的类型为ArrayList ,但从未调用ArrayList构造函数(而是将其设置为null )。

Instead of: ArrayList<Book> list = null; 代替: ArrayList<Book> list = null;

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

Additionally, list dies when it leaves the scope of addBooks() . 另外,当list离开addBooks()的范围时,它就会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();   
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM