2015-09-20 177 views
1

我正在開發一個圖書館管理程序(這是一個在線課程的任務),並且我無法在運行時解決nullpointerexception錯誤。Java初學者:nullpointerexception錯誤

這也是迄今爲止該程序:

//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) 

我明白這個錯誤是由一系列書引起的,但我真的不知道該怎麼做,我從來沒有見過一個對象實例化作爲一個方法的參數。提前致謝!

回答

5

您必須創建您的數組collection以便能夠放入任何東西。與BOOK_COLLECTION_SIZE是一個相當高的數字替換

Book[] collection; 

通過

Book[] collection = new Book[BOOK_COLLECTION_SIZE]; 

更好的是,使用List而不是數組。這樣,你就不必事先猜測你的庫的大小:

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

然後addBook可以是這樣的:

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

,你可以擺脫collectionCounter。如果你真的需要書的數量,你可以使用this.collection.size()

+1

當人們發佈答案一秒鐘之前,你可以......正是我要說的xD(除此之外,我不明白爲什麼你會在這裏使用LinkedList而不是ArrayList) 。 – bcsb1001

+1

打我吧:) –

+0

嘿,謝謝你的回答,當你說'addBook'可以看起來像這四行代碼,你的意思是,如果我選擇鏈表或者它的工作方式陣列方法呢? – Frank

3

問題在於你的Library類。 這是addBook方法使用this.collection,但這只是宣佈,因此仍然是null

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