• 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

Accessing implemented class methods through interface reference without constructor i

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a class CommonDaoImpl that implements an interface CommonDao.Now i am trying to access the getRegisterData() of CommonDaoImpl through interace CommonDao reference like this

public class CommonServiceImpl implements CommonService { CommonDao commonDao public boolean insertRegisterData(CommonBean objCommonBean) {

return commonDao.getRegisterData(objCommonBean);
}
but it is not working and thow an NullPointerException

So i slightly change my code and initialize interface reference with the constructor of implemented class CommonDao impl like this

public class CommonServiceImpl implements CommonService { CommonDao commonDao=new CommonDaoImpl();

public boolean getRegisterData(CommonBean objCommonBean) {


return commonDao.insertRegisterData(objCommonBean);
}
But i could not understand why it happens...Please help me out..Thanks in advance.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch. The code you posted is a little bit hard to read. Please use code tags, so that the forum software can properly format your code.

Member variables are initialized to null by default (if you don't initialize them explicitly). In your first example, the member variable commonDao will be null, because you don't initialize it. If you try to call a method on a variable that's null, you'll get a NullPointerException. It must be initialized to refer to some object before you try to call any methods on it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic