• Post Reply Bookmark Topic Watch Topic
  • New Topic

null pointer exception error  RSS feed

 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Why am I getting a null pointer exception where I declare setName() and setNum() inside the loop? I don't see why it's doing this.

Thanks!

-Patrick
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The problem is that when you create the phoneBook array, each of the references is set to null. You have to assign each reference to point to an actual phoneBook object before you call a method on it.
 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Keith,

Now I'm just trying to figure out why it throws a number format exception when I enter a regular 10-digit number in the phoneNum constructor??? :

Exception in thread "main" java.lang.NumberFormatException: For input string: "9044150091"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:459)
at java.lang.Integer.parseInt(Integer.java:497)
at phoneBook.setNum(phoneBook.java:25)
at phoneBook.main(phoneBook.java:36)

 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that the largest int that is representable is 2^31-1 which is 2147483647. The number you are trying to convert to an int is over 9 billion.
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because your value is greater then the max value of an integer

 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that was a suspicion of mine; I guess the best thing to do in this case is accept it as a string and format it for display? (aaa)ppp-nnnn or aaa-ppp-nnnn

-Patrick
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!