This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why Exception in this code?

 
Maya Raj
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Maya Raj
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Alejandro..
So bad of me!! didnt get that thing

anyways thanks a lot for clearing my doubt
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alejandro Galv�n
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Raghavan!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic