【问题标题】:Java beginner: nullpointerexception errorJava初学者:nullpointerexception错误
【发布时间】:2015-09-20 11:49:02
【问题描述】:

我正在开发一个图书馆管理程序(这是一个在线课程的作业),但我无法解决运行时出现的空指针异常错误。

这是目前为止的程序:

//file Library.java
public class Library {

String address;
Book[] collection;
int collectionCounter = 0;

    // Add the missing implementation to this class
    public static void main(String[] args)
    {
        // Create two libraries
        Library firstLibrary = new Library("10 Main St.");
        Library secondLibrary = new Library("228 Liberty St.");
        // Add four books to the first library
        firstLibrary.addBook(new Book("The Da Vinci Code"));
        firstLibrary.addBook(new Book("Le Petit Prince"));
        firstLibrary.addBook(new Book("A Tale of Two Cities"));
        firstLibrary.addBook(new Book("The Lord of the Rings"));
    }

    //Constructor
    public Library(String libraryName)
    {
        address = libraryName;
        collectionCounter = 0;
    }

    //Methods
    public void addBook(Book newBook)
    {
        System.out.println(this.collectionCounter);
        this.collection[this.collectionCounter] = newBook;
        this.collectionCounter += 1;
    }

还有另一个 .java 文件,用于 Book 类:

public class Book {
String title;
boolean borrowed;

    // Creates a new Book
    public Book(String bookTitle) {
        // Implement this method
        title = bookTitle;
        borrowed = false;
    }
    // Marks the book as rented
    public void rented() {
        // Implement this method
        this.borrowed = true;
    }

    // Marks the book as not rented
    public void returned() {
        // Implement this method
        this.borrowed = false;
    }

    // Returns true if the book is rented, false otherwise
    public boolean isBorrowed() {
        // Implement this method
        return this.borrowed;
    }

    // Returns the title of the book
    public String getTitle() {
        return this.title;
    }

    public static void main(String[] arguments) {

        // Small test of the Book class
        Book example = new Book("The Da Vinci Code");
        System.out.println("Title (should be The Da Vinci Code): " + example.getTitle());
        System.out.println("Borrowed? (should be false): " + example.isBorrowed());
        example.rented();
        System.out.println("Borrowed? (should be true): " + example.isBorrowed());
        example.returned();
        System.out.println("Borrowed? (should be false): " + example.isBorrowed());
    }

}

这是程序输出:

0
Exception in thread "main" java.lang.NullPointerException
    at Library.addBook(Library.java:59)
    at Library.main(Library.java:14)

我知道错误是由书籍数组引起的,但我真的不知道该怎么做,我从未见过将对象实例化作为方法的参数。提前致谢!

【问题讨论】:

    标签: java arrays object


    【解决方案1】:

    您必须创建数组collection 才能将任何内容放入其中。替换

    Book[] collection;
    

    通过

    Book[] collection = new Book[BOOK_COLLECTION_SIZE];
    

    BOOK_COLLECTION_SIZE 是一个相当高的数字。

    更好的是,使用List 而不是数组。这样您就不必事先猜测库的大小:

    List<Book> collection = new LinkedList<Book>();
    

    那么addBook可以是这样的:

    public void addBook(Book newBook)
    {
        this.collection.add(newBook);
    }
    

    你可以摆脱collectionCounter。如果您真的需要书的数量,可以使用this.collection.size() 获取。

    【讨论】:

    • 当人们比你早一秒发布答案时......正是我要说的 xD(除此之外,我不明白你为什么会在这里使用 LinkedList 而不是 @987654332 @)。
    • 嘿,谢谢你的回答,当你说addBook 可能看起来像那四行代码时,你的意思是在我选择链表方法的情况下还是它适用于数组方法也是?
    • @Frank 仅当您选择使用List 方法时,使用add 才有效。
    • 好的,谢谢,我会尽快接受答复,网站说会在 2 分钟内完成。干杯!
    【解决方案2】:

    问题在于您的Library 课程。 它是addBook 方法使用this.collection,但是这只是声明,因此仍然是null

    public void addBook(Book newBook)
    {
        System.out.println(this.collectionCounter);
        this.collection[this.collectionCounter] = newBook;
        this.collectionCounter += 1;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-01-18
      • 1970-01-01
      • 2016-03-08
      • 2013-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-18
      相关资源
      最近更新 更多