nullpointerexception error

Thread Solved

Join Date: Nov 2007
Posts: 227
Reputation: k2k is an unknown quantity at this point 
Solved Threads: 0
k2k k2k is offline Offline
Posting Whiz in Training

nullpointerexception error

 
0
  #1
Dec 20th, 2008
Member is class consisted of member informaiton such as firstname, lastname, phone etc.

would anyone please point out what this function gave me NullPointerException runtime error? I think I initialized everything.

thanks

  1. public static void read(Member box[], int count)
  2. {
  3. Scanner reader = null;
  4.  
  5. try
  6. {
  7. reader = new Scanner(new FileReader("database.txt"));
  8.  
  9. int id;
  10. String type, fn, ln, ph;
  11. Double fee;
  12.  
  13. while(reader.hasNextLine())
  14. {
  15. id= reader.nextInt();
  16. type= reader.next();
  17. fn= reader.next();
  18. ln= reader.next();
  19. ph= reader.next();
  20. fee= reader.nextDouble();
  21.  
  22. System.out.println(count);
  23. box[count]= new Member(id, type, fn, ln, ph, fee);
  24. box[count].output();
  25. //count++;
  26.  
  27. }
  28.  
  29.  
  30. }
  31.  
  32. catch(FileNotFoundException e)
  33. {
  34. System.out.println("File not Found!");
  35. }
  36.  
  37. catch(IOException e)
  38. {
  39. System.out.println("Error reading");
  40. }
  41.  
  42. catch(NoSuchElementException e)
  43. {
  44. System.out.println("done reading");
  45. }
  46.  
  47. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,176
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 479
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: nullpointerexception error

 
0
  #2
Dec 20th, 2008
I expect that argument count is related to Member array box and is holding the actual size of the array ( correct me if I'm wrong)
Then you try to read file and store retrieved data in the box array.
You try to insert first set of data into array position of count.
However as count hold the array size the variable already exceeds array boundaries as Array.size() returns number of elements (lets say 20), however last element position is actually equal to count-1 ( that will be 19 as arrays starts from zero unlike vector for example). This kick off your NullPointerException.

If your method is supposed to read a file and return retrieved data you would be better of with method that will return the array then try to locally fill in and then re-use it.

Unfortunately you may not always know amount of data to be read so you should use something more "dynamic" like ArrayList or Vector. I re-do you read method little so now instead of void you get some return therefore to call it do as follows
  1. ArrayList<Member> boxReturn = read();
and here is update read method
  1. public static ArrayList<Memeber> read()
  2. {
  3. ArrayList<Member> box = new ArrayList<Member>();
  4. Scanner reader = null;
  5.  
  6. try
  7. {
  8. reader = new Scanner(new FileReader("database.txt"));
  9.  
  10. int id;
  11. String type, fn, ln, ph;
  12. Double fee;
  13.  
  14. while(reader.hasNextLine())
  15. {
  16. id= reader.nextInt();
  17. type= reader.next();
  18. fn= reader.next();
  19. ln= reader.next();
  20. ph= reader.next();
  21. fee= reader.nextDouble();
  22.  
  23. System.out.println(count);
  24. box.add( new Member(id, type, fn, ln, ph, fee));
  25. //count++;
  26. }
  27. }
  28.  
  29. catch(FileNotFoundException e)
  30. {
  31. System.out.println("File not Found!");
  32. }
  33.  
  34. catch(IOException e)
  35. {
  36. System.out.println("Error reading");
  37. }
  38.  
  39. catch(NoSuchElementException e)
  40. {
  41. System.out.println("done reading");
  42. }
  43. finally
  44. {
  45. reader.close();
  46. }
  47.  
  48. return box;
  49. }

PS: Don't forget to close your Scanner
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 227
Reputation: k2k is an unknown quantity at this point 
Solved Threads: 0
k2k k2k is offline Offline
Posting Whiz in Training

Re: nullpointerexception error

 
0
  #3
Dec 22nd, 2008
peter_budo,
eventhou i never learned array list, it make sense to me using it. However, if i do know how many member are there? or if i want to assume there are 300 in the array and let it fill up (300 is a number that is way more than the total members) then could you suggest a way fixing my original code? i am just trying to see what went wrong and the solution of the nullpointerexception error. thanks
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: nullpointerexception error

 
0
  #4
Dec 22nd, 2008
Any exception in Java comes with a stack trace. Try to read the entire exception trace and see on which line things are going wrong. If you are using an IDE like Eclipse, use the debugging feature to inspect the values at run time. If you are using a normal text editor, look into the command line debugger JDB. BTW, have you allocated memory to the Member array box?
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC