Book对象程序中的NullPointErexception错误

时间:2014-07-24 20:34:35

标签: java

对于我的生活,我在代码中找不到这个逻辑错误......错误说明了这一点:

我有代码注释掉了我想要创建的大型程序的一部分,但对于这个问题,请忽略它。

线程“main”java.lang.NullPointerException中的异常     在SchoolTextBookSort.main(SchoolTextBookSort.java:18)

这是我的代码并提前感谢你!:

import java.util.Arrays;
import javax.swing.*;

public class SchoolTextBookSort {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    //javax.swing.SwingUtilities.invokeLater(new Runnable() {
    //    public void run() {
    //        createAndShowGUI();
    //    }
    //});

    SchoolTextBook[] theBooks = new SchoolTextBook[5];

    JOptionPane.showMessageDialog(null, theBooks);

    theBooks[0].setAuthor("Ernest Hemingway");
    theBooks[1].setAuthor("Mark Twain");
    theBooks[2].setAuthor("William Shakespeare");
    theBooks[3].setAuthor("Stephen King");
    theBooks[4].setAuthor("William Faulkner");

    theBooks[0].setTitle("A Farewell to Arms");
    theBooks[1].setTitle("The Adventures of Huckleberry Finn");
    theBooks[2].setTitle("Hamlet");
    theBooks[3].setTitle("Salem's Lot");
    theBooks[4].setTitle("The Sound and the Fury");

    theBooks[0].setPageCount(332);
    theBooks[1].setPageCount(320);
    theBooks[2].setPageCount(196);
    theBooks[3].setPageCount(439);
    theBooks[4].setPageCount(326);

    theBooks[0].setISBN(0099910101D);
    theBooks[1].setISBN(0142437174);
    theBooks[2].setISBN(0521618746D);
    theBooks[3].setISBN(0450031063);
    theBooks[4].setISBN(0679732241D);

    theBooks[0].setPrice(5.99);
    theBooks[1].setPrice(7.60);
    theBooks[2].setPrice(9.41);
    theBooks[3].setPrice(16.56);
    theBooks[4].setPrice(9.60);     

    JOptionPane.showMessageDialog(null, theBooks);

    Arrays.sort(theBooks, SchoolTextBook.BookAuthorComparator);


    //for(int i = 0; i < theBooks.length; i++) {
    //  
    //}
}

private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("Book Sorting");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    JComponent newContentPane = new RadioButtonDisplay();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
    }
}

这是SchoolTextBook类代码:

import java.util.Comparator;


public class SchoolTextBook {

private String author;
private String title;
private int pageCount;
private double ISBN;
private double price;

public String getAuthor() {
    return author;
    }
public void setAuthor(String author) {
    this.author = author;
    }

public String getTitle() {
    return title;
    }
public void setTitle(String title) {
    this.title = title;
    }

public int getPageCount() {
    return pageCount;
}
public void setPageCount(int pageCount) {
    this.pageCount = pageCount;
}

public double getISBN() {
    return ISBN;
}
public void setISBN(double iSBN) {
    ISBN = iSBN;
}

public double getPrice() {
    return price;
}
public void setPrice(double price) {
    this.price = price;
}

public static Comparator<SchoolTextBook> BookAuthorComparator 
                    = new Comparator<SchoolTextBook>() {

    public int compare(SchoolTextBook book1, SchoolTextBook book2) {

        String bookName1 = book1.getAuthor().toUpperCase();
        String bookName2 = book2.getAuthor().toUpperCase();

        //ascending order
        return bookName1.compareTo(bookName2);

    }

};

public static Comparator<SchoolTextBook> BookTitleComparator 
                    = new Comparator<SchoolTextBook>() {

    public int compare(SchoolTextBook book1, SchoolTextBook book2) {

        String bookName1 = book1.getTitle().toUpperCase();
        String bookName2 = book2.getTitle().toUpperCase();

        //ascending order
        return bookName1.compareTo(bookName2);

    }

};
}

1 个答案:

答案 0 :(得分:6)

您永远不会使用SchoolTextBook个实例初始化数组。您需要先调用SchoolTextBook构造函数来填充每个位置。

您的代码现在就是这样:

SchoolTextBook[] theBooks = new SchoolTextBook[5];

...
theBooks[0].setAuthor("Ernest Hemingway");
... more of the same ...

第一行只初始化数组;它不会使用SchoolTextBook个实例填充它(每个位置都是null)。所以你必须这样做:

SchoolTextBook[] theBooks = new SchoolTextBook[5];

...
theBooks[0] = new SchoolTextBook();
theBooks[0].setAuthor("Ernest Hemingway");
... and so on...