• 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

setting object references to null

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

I have a doubt and want to clear it. Please help.
I have created an object "sks" of class named "skillsearch" in the same class.
I have created another object "conn" of class "myConn" which is an innner class of class "skillSearch".
When I refer to "conn" object, I use it as skillsearch.sks.conn
But, when I set the reference to null i.e skillsearch.sks = null, then are all the references to other obejcts such as "conn" used in the class automatically set to null ?

Thanks,
Seema
[ February 08, 2006: Message edited by: seema prakash ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.
 
seema prakash
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. But I do get a null pointer exception when I execute this code.

I have not pasted the entire code since I just needed to clarify why am I getting null pointer exception. Is is because I set sks to null in the buttonCancelActionPerformed method?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To evaluate (skillSearch.sks.conn != null), Java has to first get the value of skillSearch.sks. Only then can it get "conn" from that object. If skillSearch.sks is null, that's fine -- there won't be an error. But then any attempt to ask the non-existent object for one of its member variables will give you a NullPointerException.

So it's not that the original object's members have been set to null -- it's that when the variable is set to null, there's no object there to ask for its members. See the difference?
 
seema prakash
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did follow what you said. I'll just explain to you in brief the operations I'm performing ...
Now, the sequence of operations that are performed are :
1. method searchskills() is called when the menu in ResumeEvalutor is selected to search for skills.
2. The GUI is created and I click on the buttonSearch and do some operations.
3. I click on buttonCancel. Here I dispose the sks object and set it to null.This means the object is refering to null but it is present ?
4. Now I want to disconnect from the database. So, I want to check if skillSearch.sks.conn is null. It is at this point I get the null pointer exception.
what may be the reason? Where am I going wrong ? Please help.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by seema prakash:
3. I click on buttonCancel. Here I dispose the sks object and set it to null.This means the object is refering to null but it is present ?

No, it means the sks variable is referring to null. The object that it used to refer to may still exist, or if nothing else refers to it then it may have been garbage collected. But you have no way of knowing about that.

4. Now I want to disconnect from the database. So, I want to check if skillSearch.sks.conn is null. It is at this point I get the null pointer exception.
what may be the reason? Where am I going wrong ? Please help.

If you are going to need the object that the sks variable refers to later, for example to check its "conn" attribute, then don't set sks to null.

Or you are calling the object's dispose() method; why isn't that closing the connection? Why do you need to check later?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic