0

I have the following program:

class Books 
{
    String title;
    String author;
}

class BookTestDrive 
{
    public static void main(String [] args) 
    {
        Books [] myBooks = new Books[3];
        int x = 0;
        myBooks[0].title = "The Grapes of Java";
        myBooks[1].title = "The Java Gatsby";
        myBooks[2].title = "The Java Cookbook";
        myBooks[0].author = "bob";
        myBooks[1].author = "sue";
        myBooks[2].author = "ian";

        while (x < 3) 
        {
            System.out.print(myBooks[x].title);
            System.out.print(" by ");
            System.out.println(myBooks[x].author);
            x = x + 1;
        }
    }
}

However, it gives me the following error when I execute it:

Exception in thread "main" java.lang.NullPointerException
    at BookTestDrive.main(Books.java:14)

I am new to Java. The code looks legitimate from my C/C++ experience...How to resolve this problem?

1
  • 2
    which of them is line 14? Commented Jun 20, 2015 at 16:39

2 Answers 2

11

The issue is that you have only created the array of books in the following lines -

Books [] myBooks = new Books[3];

You still need to initialize each element in the array to a book object before accessing them.

An example code would look like -

Books [] myBooks = new Books[3];
int x = 0;
myBooks[0] = new Books();
myBooks[0].title = "The Grapes of Java";

You need to do this for all elements in your array.

Sign up to request clarification or add additional context in comments.

Comments

2

I second the answer from @AnandSKumar (it is the direct answer to the problem after all), but because it is a matter of beauty, I could not leave without making following few changes:

public class Play {
  static public class Book {
    final public String title;
    final public String author;
    public Book(String title,String author) { 
      this.title = title; 
      this.author = author; 
    }
    @Override
    public String toString() {
      return "\""+title+"\" by "+author;
    }
  }

  public static void main(String [] args) 
  {
      Book [] books = new Book[] {
        new Book("The Grapes of Java","bob"),
        new Book("The Java Gatsby","sue"),
        new Book("The Java Cookbook","ian"),
      };

      for (Book book:books) {
          System.out.println(book);
      }
  }
}
  1. You can initialize in-line the content of your array
  2. If you represent 'a single book' then we should name the class representing it as Book and not Books to avoid confusion.
  3. We can enhance the Book class with an improved toString(), and use that instead.
  4. There is a enhanced for iterator to loop over your array.
  5. Note that the third position book in the array, also ends with a comma, although there is no element following it. This could have been a mistake, but it this case it was a deliberate choice. Makes it easier to copy-paste into next elements without introducing errors, as the Java syntax allows for it.
  6. Because once a book is created, title and author should not change anymore, it might be good to design the Book class to be 'Immutable'. For this reason a constructor was added, and the title and author fields set as final. You could also consider making them private, and provide getters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.