-1

Hi I am having some problems with my array of objects. If I assign the length of the array (of objects) to 4 it does not allow me to choose option 3 (program keeps asking for the option when I choose 3. otherwise all options works fine), if I assign it to 5 it allows me but I get null pointer exception (because the array will have an extra object with no value assign to it - null). If I print out the array just after I called the constructor. It works fine

public static void main(String[] args) 
{
    int menuOption = 0;
    int size = 4;
    BookStore[] book = new BookStore[size];

    //populate the object book for testing purpose
    book[0] = new BookStore("Now", "Me", "Aussie", 2014, 123456, 25.50, 2);
    book[1] = new BookStore("Today", "You", "UK", 1992, 023456, 20.50, 5);
    book[2] = new BookStore("Current", "Me", "Aussie", 2005, 234567, 25.00, 4);
    book[3] = new BookStore("Tomorrow", "You", "UK", 1909, 523456, 20.00, 15);
    //print out the array here, changing the index one by one, I wont have any error
    //E.g. book[0].displayDetails();

    while(menuOption !=1)
    {
        displayMenu();    //display a plain menu
        menuOption = readOption();    //read option from users keyboard

        switch(menuOption)
        {
            ..........
            case 3:
                for(BookStore b: book)
                {
                    b.displayDetails();
                    //if i assign the length of array to 4 does not allow to choose the option 3
                    //if i assign the length of array to 5 null point exception
                }
                break; 
            ........
        }
        ..............
    }
}   


public class BookStore
{
    private String title;
    private String author;
    private String publisher;
    private int year;
    private long isbn;
    private double price;
    private int quantity;

    public BookStore(String bTitle, String bAuthor, String bPublisher, int bYear, 
                     long bIsbn, double bPrice, int bQty)
    {
        title = bTitle;
        author = bAuthor;
        publisher = bPublisher;
        year = bYear;
        isbn = bIsbn;
        price = bPrice;
        quantity = bQty;
    }

    public void displayDetails()
    {
        System.out.println("Title: "+title);
        System.out.println("Author: "+author);
        System.out.println("Publisher: "+publisher);
        System.out.println("Year: "+year);
        System.out.println("Isbn: "+isbn);
        System.out.println("Price: "+price);               
        System.out.println("Quantity: "+quantity);         
        System.out.println("Total Value: "+getTotalValue());
        System.out.println("");  
    }

    public double getTotalValue()
    {
        return (double)quantity * price;
    }
    .........
}
user2839474
  • 9
  • 1
  • 2
  • "does not allow to choose the option 3" - what do you mean by that ? – Nir Alfasi Sep 16 '14 at 03:26
  • learn to use the proper typesafe `Collections` classes like `List`, `Set` and `Map` implementations instead of raw Array's. Using raw arrays is almost always wrong. –  Sep 16 '14 at 03:30
  • "does not allow to choose the option 3". I meant when I choose the option 3 the program does not display anything. It comes back to the first loop and ask user to type option. In other words, if I set the length of array to 4, program does not display anything, if I set the length to 5 it displays all the objects, however it gets the null pointer, as I create 4 books object. – user2839474 Sep 16 '14 at 10:49

1 Answers1

0

I suggest you modify BookStore, change your displayDetails() and Override toString() -

@Override
public String toString() {
  StringBuilder sb = new StringBuilder();
  sb.append("Title: "+title);
  sb.append("\nAuthor: "+author);
  sb.append("\nPublisher: "+publisher);
  sb.append("\nYear: "+year);
  sb.append("\nIsbn: "+isbn);
  sb.append("\nPrice: "+price);               
  sb.append("\nQuantity: "+quantity);         
  sb.append("\nTotal Value: "+getTotalValue());
  return sb.toString();  
}

Then you can use

System.out.println(Arrays.toString(book));

or just

for (Book b : book) {
  System.out.println(b); // <-- calls toString()
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249