This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question : object pointing to null ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question : object pointing to null ?" Watch "question : object pointing to null ?" New topic

question : object pointing to null ?

mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
please refer to the code below..
runtime exception :Nullpointerexception
but actually i really don't quite get as to what havoc is the line obj1=null;
creating ?
this line really brings in lots of .......
take out this line and all is fine. i really quite don't get it as to why this should matter.
obj1.variable or obj1.method .... what does it matter if the object itself points to a null ? let it point where it points to ? why is it effecting our variable and member references. after all for non-static variables and methods we call them via object references ? right ? If this object is a null then all the calculations go haywire ! why ? what's the big deal here.
We are not even discussing the non-static and static methods here yet. that is second. because for static references then it is ok and apparantly this object reference (obj1) does not matter.

class b {
int k=5;
static void main(String[] s)
b obj1 = new b();
obj1 = null;
int t=obj1.k;
Michael Ernest
High Plains Drifter

Joined: Oct 25, 2000
Posts: 7292

This is rather a surprising question; why should it matter what thing an object reference points to? It's the only thing that matters.
When an object reference points to null, it points to no particular instance in memory. In that space where no values exist, trying to invoke a value like ob1.k simply has no meaning. The compiler can't acutally catch this because from a compile-time point of view, the type of reference is correct. At run-time, however, there's no 'k' value that's actually there.

Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide

Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
I agree. Here's the link:
subject: question : object pointing to null ?
It's not a secret anymore!