Page 1 of 2 12 LastLast
Results 1 to 20 of 26
  1. #1
    Join Date
    Mar 2009
    Posts
    51
    Rep Power
    0

    Default library management

    i am trying a program to do library management and the problem is it is giving me exception :nullpointer exception

    i am doing this program using the packages concept

    /*this is the program of the administrator where he can add a user or add a book search for a book and lend a book or return a book and have a count of the number of books there in the library*/
    /*My problem is in the AddBook method .It is there that the exception is thrown*/
    package pack2;
    import pack1.*;
    import java.util.*;
    public class admin
    {
    Scanner s=new Scanner(System.in);
    public book b[]=new book[10];
    public String uname;
    public int userno=0,bookno,id,i,acc1,ac;
    public void AddUser()
    {
    System.out.println("Enter the name of the user:");
    uname=s.nextLine();
    System.out.println("Enter the ID number:");
    id=Integer.parseInt(s.nextLine());
    userno=userno+1;
    }
    public void AddBook()
    {
    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();
    }
    }
    public void search()
    {
    System.out.println("Give the access number for searching:");
    acc1=Integer.parseInt(s.nextLine());
    i=0;
    while(b[i].acc!=acc1)
    i=i+1;
    if(i<bookno)
    {
    System.out.println("The details of the book is:");
    b[i].display();
    }
    else
    System.out.println("The access number not found");
    }
    public void lending(int n)
    {
    System.out.println("Enter the id number of user:");
    id=Integer.parseInt(s.nextLine());
    for (i=0;i<n;i++)
    {
    System.out.println("Enter the access number of the book lended:");
    ac=Integer.parseInt(s.nextLine());
    bookno=bookno-1;
    }
    }
    public void returning()
    {
    System.out.println("Enter the access number of the book:");
    i=0;
    while(b[i].acc!=acc1)
    i=i+1;
    if(i<bookno)
    System.out.println("The deatails of the book is:");
    b[i].display();
    bookno=bookno+1;
    }
    public void count()
    {
    System.out.println("The number of books :"+bookno);
    }
    }

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

    Default

    In your previous thread, I showed you how to use code tags. I again strongly recommend that you use them when posting code in the forum, else most will not read your post.

    To reiterate, 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

    i am trying a program to do library management and the problem is it is giving me exception :nullpointer exception

    i am doing this program using the packages concept

    /*this is the program of the administrator where he can add a user or add a book search for a book and lend a book or return a book and have a count of the number of books there in the library*/
    /*My problem is in the AddBook method .It is there that the exception is thrown*/
    Java Code:
    package pack2;
    import pack1.*;
    import java.util.*;
    public class admin
    {
    Scanner s=new Scanner(System.in);
    public book b[]=new book[10];
    public String uname;
    public int userno=0,bookno,id,i,acc1,ac;
    public void AddUser()
    {
    System.out.println("Enter the name of the user:");
    uname=s.nextLine();
    System.out.println("Enter the ID number:");
    id=Integer.parseInt(s.nextLine());
    userno=userno+1;
    }
    public void AddBook()
    {
    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();
    }
    }
    public void search()
    {
    System.out.println("Give the access number for searching:");
    acc1=Integer.parseInt(s.nextLine());
    i=0;
    while(b[i].acc!=acc1)
    i=i+1;
    if(i<bookno)
    {
    System.out.println("The details of the book is:");
    b[i].display();
    }
    else
    System.out.println("The access number not found");
    }
    public void lending(int n)
    {
    System.out.println("Enter the id number of user:");
    id=Integer.parseInt(s.nextLine());
    for (i=0;i<n;i++)
    {
    System.out.println("Enter the access number of the book lended:");
    ac=Integer.parseInt(s.nextLine());
    bookno=bookno-1;
    }
    }
    public void returning()
    {
    System.out.println("Enter the access number of the book:");
    i=0;
    while(b[i].acc!=acc1)
    i=i+1;
    if(i<bookno)
    System.out.println("The deatails of the book is:");
    b[i].display();
    bookno=bookno+1;
    }
    public void count()
    {
    System.out.println("The number of books :"+bookno);
    }
    }

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

    Default

    Also, creating an array of objects is different from creating an array of primitives in that for objects you have initialize each item in the array before using them.

    For example, to create an array of int, all you have to do is declare and initialize the array:

    Java Code:
    int[] intArray = new int[10];
    intArray[3] = 25;
    With objects however, this will cause an error:
    Java Code:
    Book books = new Book[10];
    books[3].title = "foo";
    instead to get this to work, you have to initialize an item before you use it. This will work:
    Java Code:
    Book books = new Book[10];
    for (int i = 0; i < books.length; i++)
    {
      books[i] = new Book(); // initialize books in array
    }
    books[3].title = "foo";

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

    Default

    Thanks for using code tags, but your code has no formatting, no indentation. To requote myself regarding code tags:
    please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code

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

    Default

    But then how can i call for my other program in other package to which it has to refer..i want to call that and get the details from the user

    Java Code:
    package pack1;
    import java.util.*;
    public class book
    {
     public String name,author,publisher;
     public int acc;
     Scanner s=new Scanner(System.in);
     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 teh book "+name);
          System.out.println("Author of teh book "+author);
          System.out.println("Publisher of teh book "+publisher);
         } 
    }
    /*i want to call this package from that package */

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

    Default

    Actually what does this nullpointerexception means..when do we get this exception

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

    Default

    You get it when you try to use an object when it has not yet been created. For instance, you try to use b[i] objects by calling the details() method on them before you have created any b[] objects. Since there are no b objects, they are all set to null, to nothing. And trying to call a method on nothing will trip this error.

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

    Default

    /*i am getting a null pointer exception again when i am trying to access the student parts and the staff part*/


    [code]
    package pack3;
    import pack1.*;
    import pack2.*;
    import java.util.*;
    public class user extends pack2.admin
    {
    Scanner s=new Scanner(System.in);
    public int i,choice;
    admin a=new admin();
    public void staff()
    {
    System.out.println("1.Search\n2.Lending\n3.Returni ng\n4.Display");
    System.out.println("Enter the choice:");
    choice=Integer.parseInt(s.nextLine());
    switch(choice)
    {
    case 1:
    a.search();
    break;
    case 2:
    System.out.println("Enter the number of books you want to lend:");
    int le=Integer.parseInt(s.nextLine());int m=5;
    for(int i=0;i<le;i++)
    a.lending(m);
    break;
    case 3:

    a.returning();
    break;
    case 4:
    System.out.println("Enter the access number of the book:");
    i=0;
    while(b[i].acc!=acc1)
    i=i+1;
    if(i<bookno)
    System.out.println("The deatails of the book is:");
    b[i].display();
    break;
    default:
    System.out.println("wrong choice");
    }
    }

    public void student()
    {
    System.out.println("1.Search\n2.Lending\n3.Returni ng\n4.Display");
    System.out.println("Enter the choice:");
    choice=Integer.parseInt(s.nextLine());
    switch(choice)
    {
    case 1:
    a.search();
    break;
    case 2:
    System.out.println("Enter the number of books you want to lend:");
    int le=Integer.parseInt(s.nextLine());int m=3;
    for(int i=0;i<le;i++)
    a.lending(m);
    break;
    case 3:
    System.out.println("Enter the numebr of books you want to return:");
    int re=Integer.parseInt(s.nextLine());
    for(int i=0;i<re;i++)
    a.returning();
    break;
    case 4:
    System.out.println("Enter the access number of the book:");
    i=0;
    while(b[i].acc!=acc1)
    i=i+1;
    if(i<bookno)
    System.out.println("The deatails of the book is:");
    b[i].display();
    break;
    default:
    System.out.println("wrong choice");
    }
    }
    }
    [\code]

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

    Default

    /*i am getting a null pointer exception again when i am trying to access the student parts and the staff part*/


    Java Code:
    package pack3;
    import pack1.*;
    import pack2.*;
    import java.util.*;
    public class user extends pack2.admin
    {
    Scanner s=new Scanner(System.in);
    public int i,choice;
    admin a=new admin();
    public void staff()
    {
    System.out.println("1.Search\n2.Lending\n3.Returni ng\n4.Display");
    System.out.println("Enter the choice:");
    choice=Integer.parseInt(s.nextLine());
    switch(choice)
    {
    case 1:
    a.search();
    break;
    case 2:
    System.out.println("Enter the number of books you want to lend:");
    int le=Integer.parseInt(s.nextLine());int m=5;
    for(int i=0;i<le;i++)
    a.lending(m);
    break;
    case 3:
    
    a.returning();
    break;
    case 4:
    System.out.println("Enter the access number of the book:");
    i=0;
    while(b[i].acc!=acc1)
    i=i+1;
    if(i<bookno)
    System.out.println("The deatails of the book is:");
    b[i].display();
    break;
    default:
    System.out.println("wrong choice");
    }
    }
    
    public void student()
    {
    System.out.println("1.Search\n2.Lending\n3.Returni ng\n4.Display");
    System.out.println("Enter the choice:");
    choice=Integer.parseInt(s.nextLine());
    switch(choice)
    {
    case 1:
    a.search();
    break;
    case 2:
    System.out.println("Enter the number of books you want to lend:");
    int le=Integer.parseInt(s.nextLine());int m=3;
    for(int i=0;i<le;i++)
    a.lending(m);
    break;
    case 3:
    System.out.println("Enter the numebr of books you want to return:");
    int re=Integer.parseInt(s.nextLine());
    for(int i=0;i<re;i++)
    a.returning();
    break;
    case 4:
    System.out.println("Enter the access number of the book:");
    i=0;
    while(b[i].acc!=acc1)
    i=i+1;
    if(i<bookno)
    System.out.println("The deatails of the book is:");
    b[i].display();
    break;
    default:
    System.out.println("wrong choice");
    }
    }
    }

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

    Default

    Some body please answer this and help me out please i am getting a null pointer exception for the code present above for all the functions of the student and staff

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

    Default

    the code above doesn't look like it should compile. There are several variables used that have not been declared such as acct1, b array, and bookno
    Last edited by Fubarable; 03-11-2009 at 05:33 PM.

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

    Default

    the thing is i am importing from another package

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

    Default

    Quote Originally Posted by jyothi.priyanka View Post
    the thing is i am importing from another package
    that would make other classes available, but not variables.

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

    Default

    but it compiled for me i am having trouble only at the runtime..should i post the 4 packages of my program such that it will be clear

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

    Default

    perhaps. It also would help if you posted indented code. It's still very hard to read your code even with code tags. As I've stated 3 times before in your posts, the forum code formatter cannot magically format poorly or un-formatted code.

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

    Default

    Java Code:
    package pack1;
    import java.util.*;
    public class book
    {
     public String name,author,publisher;
     public int acc;
     Scanner s=new Scanner(System.in);
     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 teh book "+name);
          System.out.println("Author of teh book "+author);
          System.out.println("Publisher of teh book "+publisher);
         } 
    }
    this is the method for me to get the details and then display the details

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

    Default

    Java Code:
    package pack2;
    import pack1.*;
    import java.util.*;
    public class admin
    { 
     Scanner s=new Scanner(System.in);
     public book b[]=new book[10];
      //b[10]=new book(); 
     public String uname;
     public int userno=0,bookno,id,i,acc1,ac;
     public void AddUser()
     {
      System.out.println("Enter the name of the user:");
       uname=s.nextLine();
        System.out.println("Enter the ID number:");
        id=Integer.parseInt(s.nextLine());
         userno=userno+1;
     }
       public void AddBook()
       {
        System.out.println("Enter the number of books you want to add:");
        bookno=Integer.parseInt(s.nextLine());
         for (int i = 0; i < bookno; i++)
         {
         b[i] = new book(); // initialize books in array
         // }
           //books[3].title = "foo";
        b[i].details();
        }
      }
    public void search()
    {
     System.out.println("Give the access number for searching:");
     acc1=Integer.parseInt(s.nextLine());
      i=0;
    b[i] = new book(); 
     System.out.println("acc="+acc1);
     while(b[i].acc!=acc1 && i<10)
    {
      i=i+1;
    b[i] = new book(); 
     System.out.println(i);
    }
     if(i<bookno)
     {
     System.out.println("The details of the book is:");
     b[i].display();
     }
     else 
     System.out.println("The access number not found");
    }
      public void lending(int y,int n)
      {
       System.out.println("Enter the id number of user:");
       id=Integer.parseInt(s.nextLine());
       if(y<=n)
       {
       System.out.println("Enter the access number of the book lended:");
       ac=Integer.parseInt(s.nextLine());
       bookno=bookno-1;
       }
      }
      public void returning()
      {
      System.out.println("Enter the access number of the book:");
       i=0;
      while(b[i].acc!=acc1)
      i=i+1;
      if(i<bookno)
      System.out.println("The deatails of the book is:");
      b[i].display();
      bookno=bookno+1;
      }
                public void count()
                {
                System.out.println("The number of books :"+bookno);
                }
    }
    this is my admin class in pack2 where i get to search adduser addbooks and do the counting

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

    Default

    To solve your first problem, please see my code comments:
    Java Code:
    public class BookTest
    {
      public static void main(String[] args)
      {
        Book[] books = new Book[5]; //better to capitalize Book's first letter 
        for (int i = 0; i < books.length; i++)
        {
          // you have to first create a book *instance*
          // or object before you use it
          books[i] = new Book();
          
          // now you can call a method on the instance
          books[i].details();
        }
      }
    }
    edit: I see that you've solved this. Please indicate that you've done so in this thread somewhere!

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

    Default

    this part is coming correct now for me....it is showing null pointer exception in the search function of mine

Page 1 of 2 12 LastLast

Similar Threads

  1. Tell me jar file for library library org.bouncycastle.cms
    By 82rathi.angara in forum New To Java
    Replies: 10
    Last Post: 09-09-2008, 06:11 AM
  2. Asbru Web Content Management 6.7.1
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 04-01-2008, 05:14 PM
  3. Project management tool
    By Sudha in forum New To Java
    Replies: 0
    Last Post: 01-23-2008, 08:56 PM
  4. Asbru Web Content Management 6.6
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 09-26-2007, 01:25 PM

Posting Permissions

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