0

For our assignment we have to create a bookstore object of type array. After we create it we have to code different methods that take different actions on the array. One of the methods wants us to go through each book in the array and find the cheapest book. If two books have the same price the method should return the book that comes first in the array. This is the code for the program tester:

public class BookstoreArrayTester
{
    public static void main(String[] args)
    {
        BookstoreArray store = new BookstoreArray(6);
        
        System.out.printf("Average: %.2f%n", store.average());
        System.out.println("Expected: 0.00");                
        System.out.println("Cheapest: " + store.cheapest());
        System.out.println("Expected: ");
        System.out.println(store.toString());
        System.out.println("Expected: []");

        // Swap bad index. Should have no effect
        store.swap(-1, 1);
        store.swap(2, -1);
        store.swap(1, 5);
        store.swap(5, 2);

        System.out.printf("Average: %.2f%n", store.average());
        System.out.println("Expected: 0.00");                
        System.out.println("Cheapest: " + store.cheapest());
        System.out.println("Expected: ");

        // add four books 
        store.add(new Book("Island of the Mad", 15.20));
        store.add(new Book("Big Java", 76.95));
        store.add(new Book("New York to Dallas", 21.95));
        store.add(new Book("Swordheart", 19.25));
        
        System.out.printf("Average: %.2f%n", store.average());
        System.out.println("Expected: 33.34");                
        System.out.println("Cheapest: " + store.cheapest());
        System.out.println("Expected: Island of the Mad");
        
        // Add another book
        store.add(new Book("Little Bear", 7.95));
        
        System.out.printf("Average: %.2f%n", store.average());
        System.out.println("Expected: 28.26");
        System.out.println("Cheapest: " + store.cheapest());
        System.out.println("Expected: Little Bear");
                        
        // Add another book
        store.add(new Book("Big Bear", 7.95));
        System.out.printf("Average: %.2f%n", store.average());
        System.out.println("Expected: 24.88");
        System.out.println("Cheapest: " + store.cheapest());
        System.out.println("Expected: Little Bear");

        System.out.println(store.toString());
        System.out.println("Expected: [Book[title=Island of the Mad,price=15.2], " + 
                                      "Book[title=Big Java,price=76.95], " +
                                      "Book[title=New York to Dallas,price=21.95], " +
                                      "Book[title=Swordheart,price=19.25], " +
                                      "Book[title=Little Bear,price=7.95], " +
                                      "Book[title=Big Bear,price=7.95]]");
        
        //test swap
        store.swap(0, 3);
        store.swap(1, 5);
        System.out.printf("Average: %.2f%n", store.average());
        System.out.println("Expected: 24.88");
        System.out.println("Cheapest: " + store.cheapest());
        System.out.println("Expected: Big Bear");

        System.out.println(store.toString());
        System.out.println("Expected: [Book[title=Swordheart,price=19.25], " + 
                                      "Book[title=Big Bear,price=7.95], " +
                                      "Book[title=New York to Dallas,price=21.95], " +
                                      "Book[title=Island of the Mad,price=15.2], " +
                                      "Book[title=Little Bear,price=7.95], " +
                                      "Book[title=Big Java,price=76.95]]");
        
        store.swap(5, 0);
        System.out.println(store.toString());
        System.out.println("Expected: [Book[title=Big Java,price=76.95], " + 
                                      "Book[title=Big Bear,price=7.95], " +
                                      "Book[title=New York to Dallas,price=21.95], " +
                                      "Book[title=Island of the Mad,price=15.2], " +
                                      "Book[title=Little Bear,price=7.95], " +
                                      "Book[title=Swordheart,price=19.25]]");

        //swap bad index. Should have no effect
        store.swap(-1, 1);
        store.swap(2, -1);
        store.swap(1, 6);
        store.swap(6, 2);
        System.out.println(store);
        System.out.println("Expected: [Book[title=Big Java,price=76.95], " + 
                                      "Book[title=Big Bear,price=7.95], " +
                                      "Book[title=New York to Dallas,price=21.95], " +
                                      "Book[title=Island of the Mad,price=15.2], " +
                                      "Book[title=Little Bear,price=7.95], " +
                                      "Book[title=Swordheart,price=19.25]]");
        
        // Add another book when the store is full
        store.add(new Book("Little Bear", 5.95));
        System.out.println(store.toString());
        System.out.println("Expected: [Book[title=Big Java,price=76.95], " + 
                                      "Book[title=Big Bear,price=7.95], " +
                                      "Book[title=New York to Dallas,price=21.95], " +
                                      "Book[title=Island of the Mad,price=15.2], " +
                                      "Book[title=Little Bear,price=7.95], " +
                                      "Book[title=Swordheart,price=19.25]]");
    }
}

And this is the code for "cheapest" method that I'm working on:

public String cheapest()
{
    String cheapest;
    boolean empty = true;
    
    for (int i = 0; i < books.length; i++)
    {
        if (books[i] != null)
        {
            empty = false;
            break;
        }
        
        
    }
    if (empty == true)
    {
        return "";
    }
    else
    {
        cheapest = books[0].getTitle();
        double cheapestBook = books[0].getPrice();
        
        for(int i = 0; i < books.length; i++)
        {
            if (books[i].getPrice() < cheapestBook)
            {
                cheapestBook = books[i].getPrice();
                cheapest = books[i].getTitle();
            }
        }
    }
    return cheapest;
}

I keep getting a java.lang.NullPointerException error. Any help would be appreciated!

Bleek
  • 1
  • The stack trace will tell you where the problem is. – Jeff Holt Apr 22 '22 at 03:15
  • `NullPointerException` doesn't have anything to do with scopes of variables. – user207421 Apr 22 '22 at 03:24
  • This means you have an expression such as `a.b` where `a` is `null`. Use the line number in the stack trace to find that expression and think backward through the program execution to discover why `a` had that value rather than the one you hoped it would. If you can't do this in your head, then learn to use the java debugger to help you. – Gene Apr 22 '22 at 03:24

0 Answers0