Java program runtime error

Posted by Sam on Stack Overflow See other posts from Stack Overflow or by Sam
Published on 2011-01-15T14:16:05Z Indexed on 2011/01/15 16:53 UTC
Read the original article Hit count: 208

Filed under:
|
|
public class BooksTestDrive {
 public static void main(String[] args) {

  Books [] myBooks = new Books[3];
  int x=0;
  myBooks[0].title = "The Grapes of Jave";
  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;
  }
 }
}

This code compiles but while runtime, its giving nullpointer exception.

© Stack Overflow or respective owner

Related posts about java

Related posts about code