0

I'm trying to figure out why I'm getting the following error:

Exception in thread "main" java.lang.NullPointerException
    at Bookstore.sortArray(Bookstore.java:138)
    at Bookstore.main(Bookstore.java:188)

I think there's something wrong with my sorting method, and then my attempt to output, because that's where both the errors are pointing. Can anybody see what I'm doing wrong? This is the classic Bookstore homework assignment. It's been turned in, and this week we're adding graphics, so I'm just trying to iron-out the bugs before I move on.

My code:

* 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
Dan Zaleski
  • 77
  • 1
  • 5
  • 2
    [Of specific note, this answer.](http://stackoverflow.com/a/23852556/1079354) You don't add anything into your arrays - they're all `null`! – Makoto Sep 15 '14 at 01:31
  • Which line is line 138? What variables are being referenced there? Which one(s) is/are being used in a way which would cause trouble if they were null? How could they possibly become null? Have you tried running this in a debugger, or putting in printouts, to check the program's progress? ... Ya gotta learn these skills. – keshlam Sep 15 '14 at 01:32
  • 138: titles[i] = books[i].getBookTitle(); – Dan Zaleski Sep 15 '14 at 02:04
  • 188: bookArray = sortArray(bookArray); – Dan Zaleski Sep 15 '14 at 02:05
  • @Makoto - I do have information in my arrays. It prints out fine before I add the sorting method. That's who I am confused. – Dan Zaleski Sep 15 '14 at 02:06
  • I figured it out, thank you for pointing me in the right direction. – Dan Zaleski Sep 15 '14 at 02:14

0 Answers0