Results 1 to 12 of 12
  1. #1
    Join Date
    Mar 2009
    Posts
    51
    Rep Power
    0

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

    i get this message when i m going a library management using packages

    System.out.println("Enter the number of books you want to add:");
    bookno=Integer.parseInt(s.nextLine());
    for(i=0;i<bookno;i++)
    b[i].details();
    this is my part whre i m getting nullpointer exception

    package pack1;
    import java.util.*;
    public class book
    {
    public String name,author,publisher;
    public int acc;
    Scanner s=new Scanner(System.in);
    public book()
    {
    /*this.name=name;
    this.author=author;
    this.publisher=publisher;
    this.acc=acc;*/
    }
    public void details()
    {
    System.out.println("Enter the name of the book:");
    name=s.nextLine();
    System.out.println("Enter the author:");
    author=s.nextLine();
    System.out.println("Enter the publisher:");
    publisher=s.nextLine();
    System.out.println("Enter the access number:");
    acc=Integer.parseInt(s.nextLine());
    }
    public void display()
    {
    System.out.println("The access number of the book is "+acc);
    System.out.println("Name of the book "+name);
    System.out.println("Author of the book "+author);
    System.out.println("Publisher of the book "+publisher);
    }
    }



    this is my thing for getting the details of the book:eek:

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    1) which line throws the exception, and
    2) when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]

  3. #3
    Join Date
    Mar 2009
    Posts
    51
    Rep Power
    0

    Default

    thnx a lot i dont kw abt it so its just a beggining rite so i m improve

  4. #4
    Join Date
    Mar 2009
    Posts
    51
    Rep Power
    0

    Default

    b[i].details();
    for this line it throws exception

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by jyothi.priyanka View Post
    thnx a lot i dont kw abt it so its just a beggining rite so i m improve
    One word of unasked for advice: Please avoid unnecessary abbreviations in your posts here. I recommend this for several reasons:
    • Programming is an exercise in precision. When you communicate here (or anywhere) about programming issues and questions, you want this communication to be as clear as possible to avoid any chance for ambiguity. I'd say at least half the answers here are requests for clarification. Let's avoid that.
    • For many here, English is not their first (or second or third) language. It's hard enough for them to understand what people are posting here much less if it's couched in obscure and non-standard abbreviations.
    • Extra letters don't cost anything here, so you might as well use them.
    • It makes the poster seem a bit immature and thus for some not worthy of help.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    as for your error, you likely have not initialized all the objects in the "b" array (terrible name for a variable by the way). When you create an array of objects, it is as if you are creating an empty parking lot. If you want to use some cars in the lot, first you have to fill the lot with cars. Likewise to use objects in the array, first you have to construct some or all of the objects.

  7. #7
    Join Date
    Mar 2009
    Posts
    51
    Rep Power
    0

    Default

    ok..thanks a lot i m sory if i hurt u

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by jyothi.priyanka View Post
    ok..thanks a lot i m sory if i hurt u
    hurt? no. continue to use abbreviations and thus annoy? yes, you're getting there. keep it up and to the do-not-help list you go.

  9. #9
    Join Date
    Mar 2009
    Posts
    51
    Rep Power
    0

    Default

    i didnot get what is the do-not-help list

  10. #10
    Join Date
    Mar 2009
    Posts
    51
    Rep Power
    0

    Default

    so for my error should i go on with a array which is also object...can i do that

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    you already have an array called b. I see you using it without seeing that you've constructed an array yet and without seeing if you initialized every object in the array. please read up in your text book on using array of objects.

  12. #12
    Join Date
    Mar 2009
    Posts
    51
    Rep Power
    0

Similar Threads

  1. Replies: 15
    Last Post: 10-12-2010, 05:51 PM
  2. Exception in thread "main" java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 0
    Last Post: 02-24-2009, 01:19 PM
  3. Replies: 1
    Last Post: 02-17-2009, 10:54 AM
  4. Replies: 3
    Last Post: 01-29-2008, 01:37 AM
  5. Replies: 1
    Last Post: 07-16-2007, 06:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •