JAVA Null指针异常getTitle方法,我找不到它

时间:2014-01-18 14:29:04

标签: java nullpointerexception

编码时没有错误,但是当我运行它时,它说

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];
    }
}

1 个答案:

答案 0 :(得分: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会尝试调用null,因为title没有static


要解决此问题,请从booksInShelf字段中删除{{1}}修饰符。