Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Exception in thread "main" java.lang.NullPointerException

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Exception in thread "main" java.lang.NullPointerException

    class Books{
    String title;
    String author;
    }
    class BooksTestDrive{
    public static void main(String [] args){
    Books[] myBooks=new Books[3];
    int x=0;
    myBooks[0].title="Harry Potter";
    myBooks[1].title="Angels and Demons";
    myBooks[2].title="Revolution 2020";
    myBooks[0].author="J.K.Rowling";
    myBooks[1].author="Dan Brown";
    myBooks[2].author="Chetan Bhagat";
    while(x<3){
    System.out.println(myBooks[x].title);
    System.out.print(" by ");
    System.out.print(myBooks[x].author);
    x=x+1;
    }
    }
    }
    Last edited by JavaPF; October 31st, 2011 at 05:50 AM. Reason: Use highlight tags!


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    50
    My Mood
    Fine
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Your myBooks objects are not initialized. You need to call the constructor on each object inside the vector. eg. mybooks[0] = new Books();

    application context
    Last edited by daniel.j2ee; December 13th, 2011 at 04:59 PM.

Similar Threads

  1. Replies: 7
    Last Post: May 30th, 2011, 09:11 AM
  2. Exception in thread "main" java.lang.NullPointerException
    By isun in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 28th, 2011, 09:22 AM
  3. Exception in thread "main" java.lang.NullPointerException
    By Tsark in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 11th, 2011, 08:39 AM
  4. Replies: 16
    Last Post: August 27th, 2010, 03:30 PM
  5. Please help! Exception in thread "main" java.lang.NullPointerException
    By Arutha2321 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 18th, 2009, 02:25 AM