2011-01-30 7 views
2

このコードを実行すると、NULLポインタが返されます。その自明の2つのクラスは、本と図書館をモデル化するために使用されます。オブジェクトの配列を使用しているときにJavaのNULLポインタ例外が発生しました

私が間違っている場所を誰かが教えてくれれば幸いです。

public class Library { 


    private String address; 
    private final static String workinghours = "9AM to 5PM"; 
    private Book[] bookcollection = new Book[6]; 
    private static int numberofbooks = 0; 

    public Library(String libraryaddress) { 
     address = libraryaddress; 
    } 

    public static void printOpeningHours() { 
     System.out.println(workinghours); 
    } 

    public void addBook(Book newaddition) { 
     bookcollection[numberofbooks] = newaddition; 
     numberofbooks++; 
    } 

    public void printAddress() { 
     System.out.println(address); 
    } 

    public void borrowBook(String bookname) { 
     for (int i = 0; i < bookcollection.length; i++) { 

      if (bookcollection[i].getTitle().equals(bookname)&&(!(bookcollection[i].isBorrowed()))) 
      { 
       bookcollection[i].borrowed(); 
       break; 
      } 

     } 

    } 

    public void returnBook(String bookname) { 
     for (int i = 0; i < bookcollection.length; i++) { 

      if (bookcollection[i].getTitle().equals(bookname)) { 
       bookcollection[i].returned(); 
       break; 
      } 

     } 

    } 

    public void printAvailableBooks() { 
     for (int i = 0; i < bookcollection.length; i++) { 

      if (!(bookcollection[i].isBorrowed())) { 
       System.out.println(bookcollection[i].getTitle()); 
      } 

     } 

    } 

    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")); 

     // Print opening hours and the addresses 
     System.out.println("Library hours:"); 
     printOpeningHours(); 
     System.out.println(); 

     System.out.println("Library addresses:"); 
     firstLibrary.printAddress(); 
     secondLibrary.printAddress(); 
     System.out.println(); 

     // Try to borrow The Lords of the Rings from both libraries 
    /* System.out.println("Borrowing The Lord of the Rings:"); 
     firstLibrary.borrowBook("The Lord of the Rings"); 
     firstLibrary.borrowBook("The Lord of the Rings"); 
     secondLibrary.borrowBook("The Lord of the Rings"); 
     System.out.println();*/ 

     // Print the titles of all available books from both libraries 
     System.out.println("Books available in the first library:"); 
     firstLibrary.printAvailableBooks(); 
     System.out.println(); 
     System.out.println("Books available in the second library:"); 
     secondLibrary.printAvailableBooks(); 
     System.out.println(); 

     // Return The Lords of the Rings to the first library 
     System.out.println("Returning The Lord of the Rings:"); 
     firstLibrary.returnBook("The Lord of the Rings"); 
     System.out.println(); 

     // Print the titles of available from the first library 
     System.out.println("Books available in the first library:"); 
     firstLibrary.printAvailableBooks(); 
    } 
} 

 

//Library uses objects of class book as members 

public class Book { 

    String title; 
    boolean borrowed; 

    // Creates a new Book 
    public Book(String bookTitle) { 

     title = bookTitle; 
     borrowed = false ; 
    } 

    // Marks the book as rented 
    public void borrowed() { 

     borrowed = true; 
    } 

    // Marks the book as not rented 
    public void returned() { 

     borrowed = false; 
    } 

    // Returns true if the book is rented, false otherwise 
    public boolean isBorrowed() { 

     return ((borrowed) ? true : false); 

    } 

    // Returns the title of the book 
    public String getTitle() { 

     return 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.borrowed(); 
     System.out.println("Borrowed? (should be true): " 
       + example.isBorrowed()); 
     example.returned(); 
     System.out.println("Borrowed? (should be false): " 
       + example.isBorrowed()); 
    } 
} 
+2

2件のコメント:NPEはどこで手に入りますか?以前の質問に対する回答の一部を受け入れてください。 – aioobe

+0

あなたのスタックを見ることができ、どの行が例外をスローします。 –

+0

私は駐車場の類推が好きです。駐車場のようなオブジェクトの配列を考えてみましょう。あなたは車の1つを運転しようとする前に車(物体)でそれを記入しなければなりません(物体の1つを使用してください)。そして、私はあなたの質問を取りに戻っていくつかの答えを受け入れるべきであるという上記のコメントもまた二番目です。これは他の人が将来あなたを助けることを奨励します。 –

答えて

2

for (int i = 0; i < bookcollection.length; i++) { 

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

それが今現状では

のように見えるすべてのループを変更してみてください、あなたは上 bookcollection[i].getTitle()をしようとしますeまだ値が割り当てられていないブック配列内にntry(つまり、まだnullが含まれています)。

+0

ありがとう、私は間違いを認識した。私はNULLポインタの例外を避けるために良いプログラミングが必要だと思います。私は今forループでnumberofbooksを使用しており、numberofbooks == 0 - > "no book in catalog"のときにif節を持っています。 – maverick

+0

はい、私はそれを見ました。なぜなら、私は長く説明して答えを説明することを気にしませんでした:-) – aioobe

3

private Book[] bookcollection = new Book[6];で書籍の配列を作成すると、配列内の各Bookは、最初にnullになります。したがって、nullをチェックせずに配列をループすると、NullPointerExceptionが得られます。これが起こる場所の一つはprintAvailableBooks()である:

for (int i = 0; i < bookcollection.length; i++) { 
    if (!(bookcollection[i].isBorrowed())) { // NPE if bookcollection[i] is null! 
     .... 

null本の数と一致する必要がありますされ、numberofbooksに対するこの、ループを修正するには。私は留意する必要があると考えてい

0

もう一つのポイントは、

NUMBEROFBOOKS

は静的変数であってはならないということです。 これは、firstLibraryとsecondLibraryに共通するためです。

firstLibraryに4冊の本を追加し、次にsecondLibraryに本を追加すると、配列の5番目の位置に格納されているとします。したがって、secondLibraryの最初の4つのインデックスにはヌルが含まれ、5番目のインデックスには書籍が含まれます。

これに起因して発生する可能性のある別の問題は、ArrayIndexOutofBoundExceptionです。

+0

はい、私は理解しました、コードを改訂しながらそれを変更しました。それにもかかわらず、良いキャッチ。 – maverick

関連する問題