java.lang.NullPointerException:null

时间:2015-04-05 23:37:57

标签: java nullpointerexception

所以,基本上我有一个

的图书馆系统
    private int numberOfBooks = 0;

    public int getNumberOfBooks()
{
    return numberOfBooks;
}

当然,当您在库中添加新书时,numberOfBooks会增加1.我的其他类Student具有以下代码:

    private LibrarySystem libraryBooks;

    public void searchByISBN()
{
    int i = 0;
    boolean found = false;
    while(i<libraryBooks.getNumberOfBooks()) //THE PROBLEM IS HERE
    {
        if(books[i].getISBN().equals(in.next()))
        {
            found = true;
            break;
        }
        i++;
    }
    if(found)
    {
        System.out.println("\n Book info: \n ISBN: " + books[i].getISBN() + 
                            "\n Title: " + books[i].getTitle() +
                            "\n Author: " + books[i].getAuthor() +
                            "\n Subject: " + books[i].getSubject());
                        }
       else System.out.println("There is no book with such ISBN");
}

我收到了Java.lang.NullPointerException,我不知道为什么。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

在您的函数中,输入以下内容:

libraryBooks = new LibrarySystem();

因为您尚未初始化它,libraryBooks为空