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

    Default null pointer exception

    i am getting null pointer exception at runtime what should be done my program is given below...it is all about library mangement


    /*when ever i am trying to search for the book after accessing the number of the book it is giving me null pointer exception and even in display and return also it is giving me null pointer exception*/
    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();
     //book b[]=new b();
     public void staff()
     {
      System.out.println("1.Search\n2.Lending\n3.Returning\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(i,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)
        b[i] = new book();
        i=i+1;
         if(i<bookno)
         System.out.println("The details 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.Returning\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(i,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)
        b[i] = new book();
        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");
     }
    }
    }

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

    Default

    Please don't repost the same question twice. I will ask the moderators to delete this thread.

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    aaaaaaaaaa

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

    Default

    exception is coming in the the search method and the display method and the returning method

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

    Default

    i did not get what aaa means

  6. #6
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    What line you're having the npe?
    I die a little on the inside...
    Every time I get shot.

  7. #7
    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);
                }
    }
    in this search function i am having my npe

  8. #8
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    No shit Sherlock...

    Wait... this code is different than the one in your first post, why didn't you post this the first time? or include it in your first post.

    I meant.. what line... the compiler says that.
    I die a little on the inside...
    Every time I get shot.

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

    Default

    Actually i am doing this program using the concept of packages...so i am getting the exception in
    at pack2.admin.search(admin.java:41)
    at pack3.user.student(user.java:58)
    at pack4.lib.main(lib.java:55)

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

    Default

    OK, too many threads on the same subject... I'm bailing out.

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

    Default

    ok thats okk i just wanted help from you and i dont think so i got my help..i might have been a starter and this is the way you people help me out

  12. #12
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Java Code:
    while(b[i].acc!=acc1 && i<10)
    This means the loop'll break if b[i].acc != acc1 - so it won't just skip that one, it will stop, so skip that one and all after it.
    Is that what you want or you made some kind of mistake?

    I guess that's the part where the error is occurring- though it would be useful if you could highlight line 41.
    I die a little on the inside...
    Every time I get shot.

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

    Default

    ok thats okk i just wanted help from you and i dont think so i got my help..i might have been a starter and this is the way you people help me out
    Having more than one active thread on the same question is not being respectful of those helping you for free as you are asking them to do extra work that they shouldn't be doing -- to be following more than one thread. Please be respectful of us and keep it all in one thread only.

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 07:48 PM
  2. Null Pointer Exception
    By ScKaSx in forum New To Java
    Replies: 1
    Last Post: 01-24-2009, 12:27 PM
  3. Null Pointer Exception
    By Jacinth in forum New To Java
    Replies: 4
    Last Post: 01-22-2009, 02:47 PM
  4. null pointer exception
    By cityguy503@yahoo.com in forum New To Java
    Replies: 4
    Last Post: 08-22-2008, 08:22 PM
  5. getting a null pointer exception
    By Rjava in forum XML
    Replies: 4
    Last Post: 07-16-2008, 06:56 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
  •