File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Object Reference 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 » Java » Beginning Java
Bookmark "Object Reference to Null " Watch "Object Reference to Null " New topic

Object Reference to Null

Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
I have very small doubt.

Integer intvar;

while(...){; //Here Error comes,Var->Not initialized

but if
i do like Integer intvar=null;
It works .Althought i am not 'new'ing the intvar objectRef.
But how it works with null ,which also means pointing to nothing

Thank you
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
I think you're declaring intvar as a local object within a method.

A local variable must be initialized before using. If not you will have a compile time error which states that the variable is not initialized.

In your second attempt you initialize your object to null. So it's initialized.

Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
kirba devi
Ranch Hand

Joined: Jun 29, 2007
Posts: 50
Hi Remko,
Local variable must be initialized before using.
the statement what you said is correct....
by looking at the code

Integer intvar;

while(...){; //Here Error comes,Var->Not initialized

dont consider intvar as a local object."See where it is declared".it
is an instance reference variable(for the object goi to create) then it will automatically get initialized to null..

may be some wrong withe the coding
if he typed the code like

Integer intvar..

then it is a local variable...

if i am wrong with the above statements please let me know...

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Actually the example you showed should compile just fine (except for the syntax errors). Look at this:
This example compiles without error as shown, however if you uncomment the last line you will get a compile time error:
What's happening is that you can't use a reference until its been definitely assigned, the compiler cannot guarantee that the while loop will ever execute, therefore it cannot guarantee that 'var' will be definitely assigned.
[ June 30, 2007: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Just to throw another log on this fire, this compiles without error:
because the compiler can infer that the loop will definitely execute at least once.
[ June 30, 2007: Message edited by: Garrett Rowe ]
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
I may not know the right way to say it, but, I have noticed in several texts that, even though in the order of your method's sequence you ARE setting the variable before it is used, IF it is down in nested logic, it can STILL throw the compiler and cause a warning to be generated. Those books say that the correct thing to do in these cases is to initialize the variable to some reasonable value when it is declared.

SCJP - 86% - June 11, 2009
I agree. Here's the link:
subject: Object Reference to Null
It's not a secret anymore!