为了我的一生,我无法在代码中找到此逻辑错误……该错误说明如下:
我已将代码注释掉,这就是我要创建的较大程序的一部分,但是对于此问题,请忽略它。
线程“主”中的异常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);
}
};
}
最佳答案
您永远不会使用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...