my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why Exception in this code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why Exception in this code?" Watch "Why Exception in this code?" New topic
Author

Why Exception in this code?

Maya Raj
Greenhorn

Joined: Nov 22, 2007
Posts: 12
This code is giving me NullPointerException on the line where //HERE is placed. I'm not able to understand, why it is not calling getValue() method in ClassC ?



I came accross this code in some free mock-exam.I'm sorry I dont remember the exact site.

Pls help me..
Thanks in advance..
Alejandro Galv�n
Greenhorn

Joined: Nov 17, 2007
Posts: 17
Hi Maya:


It�s easy. Your exception is thrown because you have declared a classC reference variable which doesn�t point to any object of type ClassC. So, you are calling a method which can�t be accessed, remember you doesn�t have an object that can call the method, you only have a reference variable, but to do this
public ClassC classC;
doesn�t create an object.

Look at ClassA, you are instantiating an object in ClassB classB=new ClassB();, try to do the same in ClassB, write
ClassC classC=new ClassC();, it must compile and run fine.

Regards,


SCJP5.0
Maya Raj
Greenhorn

Joined: Nov 22, 2007
Posts: 12
Thanks Alejandro..
So bad of me!! didnt get that thing

anyways thanks a lot for clearing my doubt
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

In class B, you have created Class C reference type, but it has not been initialized, So from memory point of view :

classC = null

Since it was an instance variable so compiler did not complain and you got NullPointerException because you did following from JVM point of view :

null.methodC()


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Maya Raj:
Thanks Alejandro..
So bad of me!! didnt get that thing

anyways thanks a lot for clearing my doubt


Alejandro Galv�n 's explanation is perfect.

Fine, if you want to access an instance method what you need is an instance.

When you get an instance is actually when you create an instance. How do you create an instance? Obviously by invoking through new, like



The above step creates a brand new object (instance) of <className> and assigns it to the <referenceVarName>.

In your Class B, what you have is just a reference variable



To make that work, you need to create an instance/object and assign the object to this reference variable as what you have correctly done in you class A's getValue() method.

Just cross check the same and compare. You will get it!

Since you do NOT have any real object existing and you are trying to so-called-imaginary-object through a reference variable classC in the getValue() method of ClassB, the runtime environment does have any real object to drill down. According to it, the classC reference variable is NULL. Means, it points to nowhere. So it gives you a traditional, famous NullPointerException.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Alejandro Galv�n
Greenhorn

Joined: Nov 17, 2007
Posts: 17
Thanks Raghavan!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why Exception in this code?
 
Similar Threads
Why runtime exception thrown is this code