• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why Exception in this code?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Alejandro..
So bad of me!! didnt get that thing

anyways thanks a lot for clearing my doubt
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Raghavan!!!
reply
    Bookmark Topic Watch Topic
  • New Topic