我试图弄清楚为什么我会收到以下错误:
Exception in thread "main" java.lang.NullPointerException
at Bookstore.sortArray(Bookstore.java:138)
at Bookstore.main(Bookstore.java:188)
我认为我的排序方法有问题,然后我尝试输出,因为这两个错误都指向了。任何人都可以看到我做错了什么吗?这是经典的书店作业。它已经上交了,本周我们正在添加图形,所以我只是想在继续之前解决这些问题。
我的代码:
* Currency Formating Import */
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Arrays;
/* Declare Secondary Class */
class Book {
/* Declare Variables */
private String isbn;
private String bookTitle;
private String authorName;
private int yearPublished;
private String publisherName;
private double bookPrice;
/* Get Methods */
public String getIsbn() {
return isbn;
}
public String getBookTitle() {
return bookTitle;
}
public String getAuthorName() {
return authorName;
}
public int getYearPublished() {
return yearPublished;
}
public String getPublisherName() {
return publisherName;
}
public double getBookPrice() {
return bookPrice;
}
/* Set Methods */
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public void setBookTitle(String bookTitle) {
this.bookTitle = bookTitle;
}
public void setAuthorName(String authorName) {
this.authorName = authorName;
}
public void setYearPublished(int yearPublished) {
this.yearPublished = yearPublished;
}
public void setPublisherName(String publisherName) {
this.publisherName = publisherName;
}
public void setBookPrice(double bookPrice) {
this.bookPrice = bookPrice;
}
/* Constructor */
public Book (String isbn, String title, String author, int year, String publisher, double price) {
this.isbn = isbn;
this.bookTitle = title;
this.authorName = author;
this.yearPublished = year;
this.publisherName = publisher;
this.bookPrice = price;
}
public String toString() {
return "The Title of this Book is: " + bookTitle + "\n" + "The ISB number is: " + isbn + "\n" + "The name of the author is: " + authorName + "\n" + "This book was published in " + yearPublished + " by " + publisherName + ", and costs $" + bookPrice + "\n";
}
} // End Class Book
/* Declare Secondary Class */
class Ebook extends Book {
/* Declare Variables */
private String website;
/* Get Methods */
public String getWebsite() {
return website;
}
/* Set Methods */
public void setWebsite(String website) {
this.website = website;
}
/* Constructor */
public Ebook(String isbn, String title, String author, int year,
String publisher, double price, String website) {
super(isbn, title, author, year, publisher, price);
this.website = website;
}
public double calculateDiscount() {
return getBookPrice() * .10;
}
public String toString() {
return super.toString() + "This book is also available online through " + getWebsite() + " and is discounted $" + calculateDiscount() + "\n";
}
} // End class Ebook
/* Declare Main Class */
public class Bookstore {
/* Method to Sort Books */
public static Book [] sortArray(Book[] books) {
String[] titles = new String[books.length];
Book[] sortedBooks = new Book [books.length];
for (int i = 0; i < books.length; i++){
titles[i] = books[i].getBookTitle();
}
Arrays.sort(titles, String.CASE_INSENSITIVE_ORDER);
for (int i = 0; i < books.length; i++){ //Product Array
for (int j = 0; j < titles.length; j++){ //Names Array
if (books[i].getBookTitle().equalsIgnoreCase(titles[j])) {
sortedBooks[j] = books[i];
break;
}
}
}
return sortedBooks;
}
/* Method to Calculate Inventory */
public static double calculateInventoryTotal(Book[] books){
double total = 0;
for (int i = 0; i < books.length; i++) {
total += books[i].getBookPrice();
}
return total;
}
/* Main Method */
public static void main(String[] args)
{
/* Book Objects */
Book [ ] bookArray;
bookArray = new Book [5];
bookArray[0] = myBook();
bookArray[1] = myBook1();
bookArray[2] = myBook2();
bookArray[3] = myBook3();
bookArray[4] = myBook4();
Book myBook = new Book("TD45454545", "My First Java Stuff", "Dan Zaleski", 2014, "UPX", 19.99);
Book myBook1 = new Book("KY67676767", "Hello From Boston", "Joe Smith", 1999, "Random House", 19.99);
Book myBook2 = new Book("NCC7890987", "I Hate Onions", "John Cooper", 1990, "Random House", 9.99);
Ebook myBook3 = new Ebook("YY00000000", "Yo Dawg", "Jim Fix", 1980, "Penguin", 29.99, "http://amazon.com");
Ebook myBook4 = new Ebook("HB12345678", "Lottsa Books", "Wille Sargent", 2010, "Wherever", 129.99, "http://amazon.com");
/* Sort Items */
bookArray = sortArray(bookArray);
/* Calculate Inventory */
double inventoryTotal = calculateInventoryTotal(bookArray);
/* Declare Number Format */
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
/* Print Sorted Inventory */
System.out.println("Bookstore Items: " + "\n");
for (int i = 0; i < bookArray.length; i++){
System.out.println(bookArray[i]);
System.out.println();
}
/* Output Total of Inventory */
System.out.println("Total Inventory Value: " + nf.format(inventoryTotal));
/* Print Declarations */
System.out.println(myBook);
System.out.println(myBook1);
System.out.println(myBook2);
System.out.println(myBook3);
System.out.println(myBook4);
} // End Main Method
private static Book myBook4() {
return null;
}
private static Book myBook3() {
return null;
}
private static Book myBook2() {
return null;
}
private static Book myBook1() {
return null;
}
private static Book myBook() {
return null;
}
} // End Class Bookstore