Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why runtime exception thrown is this code

 
Divyya Joshi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code throws RuntimeException and i am not able to figure out the cause of it.
kindly help me.
Here's the code
 
John Stark
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do



but classC was not initialized and has therefore the default value null. This gives a NullPointerException. You have to create a ClassC object at some point.

John
 
Meherdad Bomanbehram
Ranch Hand
Posts: 142
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Divyya,
In ClassB you have declared but not instantiated ClassC .To refer to ClassC you have to either create an instance of the class or make the ClassC method static .If you choose to make the getValue() method static you will require to make the String variable value static too .
 
Divyya Joshi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks John and MeherDad
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic