Results 1 to 20 of 26
Thread: library management
- 03-11-2009, 02:25 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 51
- Rep Power
- 0
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);
}
}
-
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 [code] above your pasted code and the tag [/code] below your pasted code like so:
Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
- 03-11-2009, 02:31 PM #3
Member
- Join Date
- Mar 2009
- Posts
- 51
- Rep Power
- 0
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); } }
-
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;
Java Code:Book books = new Book[10]; books[3].title = "foo";
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";
-
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
- 03-11-2009, 02:40 PM #6
Member
- Join Date
- Mar 2009
- Posts
- 51
- Rep Power
- 0
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); } }
- 03-11-2009, 02:57 PM #7
Member
- Join Date
- Mar 2009
- Posts
- 51
- Rep Power
- 0
Actually what does this nullpointerexception means..when do we get this exception
-
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.
- 03-11-2009, 03:12 PM #9
Member
- Join Date
- Mar 2009
- Posts
- 51
- Rep Power
- 0
/*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]
- 03-11-2009, 03:13 PM #10
Member
- Join Date
- Mar 2009
- Posts
- 51
- Rep Power
- 0
/*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"); } } }
- 03-11-2009, 05:08 PM #11
Member
- Join Date
- Mar 2009
- Posts
- 51
- Rep Power
- 0
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
-
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.
- 03-11-2009, 05:35 PM #13
Member
- Join Date
- Mar 2009
- Posts
- 51
- Rep Power
- 0
the thing is i am importing from another package
-
- 03-11-2009, 05:39 PM #15
Member
- Join Date
- Mar 2009
- Posts
- 51
- Rep Power
- 0
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
-
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.
- 03-11-2009, 05:45 PM #17
Member
- Join Date
- Mar 2009
- Posts
- 51
- Rep Power
- 0
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); } }
- 03-11-2009, 05:49 PM #18
Member
- Join Date
- Mar 2009
- Posts
- 51
- Rep Power
- 0
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); } }
-
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(); } } }
- 03-11-2009, 05:54 PM #20
Member
- Join Date
- Mar 2009
- Posts
- 51
- Rep Power
- 0
Similar Threads
-
Tell me jar file for library library org.bouncycastle.cms
By 82rathi.angara in forum New To JavaReplies: 10Last Post: 09-09-2008, 06:11 AM -
Asbru Web Content Management 6.7.1
By Java Tip in forum Java SoftwareReplies: 0Last Post: 04-01-2008, 05:14 PM -
Project management tool
By Sudha in forum New To JavaReplies: 0Last Post: 01-23-2008, 08:56 PM -
Asbru Web Content Management 6.6
By JavaBean in forum Java SoftwareReplies: 0Last Post: 09-26-2007, 01:25 PM
Bookmarks