File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why runtime exception thrown is this code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why runtime exception thrown is this code " Watch "Why runtime exception thrown is this code " New topic
Author

Why runtime exception thrown is this code

Divyya Joshi
Ranch Hand

Joined: Jul 15, 2010
Posts: 102
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

Joined: Jul 19, 2011
Posts: 185
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

Joined: May 28, 2007
Posts: 140

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

Joined: Jul 15, 2010
Posts: 102
Thanks John and MeherDad
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why runtime exception thrown is this code