Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

null pointer exception error  RSS feed

 
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
 
Ranch Hand
Posts: 2410
  • 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: 2410
  • 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.
 
Ranch Hand
Posts: 893
Java Tomcat Server Ubuntu
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!