2014-01-18 68 views
-2

而對其進行編碼,但是當我運行它,它說JAVA空指針異常的getTitle方法,我找不到它

Exception in thread "main" java.lang.NullPointerException 
    at homework.Book.getTitle(Book.java:36) 
    at homework.BookMain.main(BookMain.java:61) 
Java Result: 1 
*********************************** 
package homework; 


public class BookMain { 
    public static void main(String[] args){ 
     int i; 
     int option; 
     Book[] bookSet = new Book[20]; 
     bookSet[0] = new Book("I dare you","Joyce Meyer",2007); 
     bookSet[1] = new Book("Straight from the Heart","Rev. Fr. Mario Jose C. Ladra",2012); 
     bookSet[2] = new Book("Deliverance From Fear","Bob Buess",1993); 
     bookSet[3] = new Book("Extraordinary Book of Facts","Bathroom Readers' Institute",2006); 
     bookSet[4] = new Book("Fat Kid Rules the World","K.L. Going",2003); 
     Book.numberOfBooks = 5; 

     Book getter = new Book(); 

     for (i=0; i<Book.numberOfBooks; i++) 
     { 
      System.out.println(getter.getTitle(bookSet[i])+" "+getter.getAuthor(bookSet[i])+" "+getter.getYear(bookSet[i])); 
     } 
     System.out.println(); 

     for (i=0; i<Book.numberOfBooks; i++) 
     { 
      if(getter.getYear(bookSet[i])>2000) 
       System.out.println(getter.getTitle(bookSet[i])+" "+getter.getAuthor(bookSet[i])+" "+getter.getYear(bookSet[i])); 
     } 

     bookSet[5] = new Book("The Lake of Dead Languages","Carol Goodman",2002); 

     Shelf shelf1 = new Shelf("Shelf1","Bedroom"); 
     Shelf shelf2 = new Shelf("Shelf2","Living room"); 
     Shelf shelf3 = new Shelf("Shelf3","Basement"); 

     Shelf placer = new Shelf(); 
     placer.insertBook(shelf1,bookSet[1]); 
     placer.insertBook(shelf1,bookSet[2]); 

     placer.insertBook(shelf2,bookSet[3]); 
     placer.insertBook(shelf2,bookSet[4]); 

     placer.insertBook(shelf1,bookSet[5]); 
     placer.insertBook(shelf1,bookSet[0]); 

     System.out.println(placer.getShelfName(shelf1)+"  "+placer.getLocation(shelf1)); 
     Book aBookInShelf = new Book(); 

     for(i=0; i<shelf1.booksInShelf; i++) 
     { 
      aBookInShelf = placer.pickBook(shelf1, i); 
      System.out.println(getter.getTitle(aBookInShelf)+" "+getter.getAuthor(aBookInShelf) 
        +" "+getter.getYear(aBookInShelf)); 
     } 

    } 
} 

=========================================== 
Books class: 
package homework; 


public class Book { 
    private String title; 
    private String author; 
    private int year; 
    public static int numberOfBooks = 0; 

    public Book(String title, String author, int year){ 
     this.title=title; 
     this.author=author; 
     this.year=year; 
    } 

    public Book(){ 

    } 

    public String getAuthor(Book target){ 
     return target.author; 
    } 
    public int getYear(Book target){ 
     return target.year; 
    } 
    public String getTitle(Book target){ 
     return target.title; 
    } 
    public static void main(String[] args){ 

    } 
} 

===================================== 
Shelves class: 

package homework; 

public class Shelf { 
    private String name; 
    private String location; 
    public static int booksInShelf=0; 
    private Book[] shelfBooks = new Book[20]; 

    public Shelf(String name, String location){ 
     this.name = name; 
     this.location = location; 
    } 

    public Shelf(){ 

    } 

    public String getShelfName(Shelf target){ 
     return target.name; 
    } 

    public String getLocation(Shelf target){ 
     return target.location; 
    } 
    public void insertBook(Shelf target, Book aBook){ 
     target.shelfBooks[target.booksInShelf] = aBook; 
     target.booksInShelf++; 
    } 
    public Book pickBook(Shelf target, int nthBook){ 
     return target.shelfBooks[nthBook]; 
    } 
} 
+3

你正在做東西倒退。爲什麼要傳遞目標?爲什麼不能得到實際的對象的字段? –

+0

我們我們不允許使用公共,所以我做了方法返回屬性 – user3202989

+1

你在思考倒退。而不是'public String getAuthor(Book target)'使其成爲'public String getAuthor()'。代替'getter.getAuthor(someBook)',執行'someBook.getAuthor()'並返回'this'對象的字段值。 –

回答

1

旁邊的getter/setter設計瘋狂沒有錯誤它似乎NullPointerException是由於booksInShelf是靜態的,這意味着它屬於整個Shelf類(此類的此類共享值的實例),因此,當您將書籍添加到Shelf並增加此字段時,它會增加Shelf的所有實例。

因爲在循環

for(i=0; i<shelf1.booksInShelf; i++) 

你迭代甚至超過了未設置還和仍nulls位置。現在

 aBookInShelf = placer.pickBook(shelf1, i); 
     System.out.println(getter.getTitle(aBookInShelf)+" "+getter.getAuthor(aBookInShelf) 
       +" "+getter.getYear(aBookInShelf)); 

就領這null,並用它裏面getter.getTitle(null)這將嘗試調用return null.title將區分NPE因爲null沒有title


爲了解決這個問題,從booksInShelf字段中刪除static修改。