• 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

Object Reference to Null

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have very small doubt.

Integer intvar;

while(...){
intvar=it.next(); //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
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Remko,
Local variable must be initialized before using.
the statement what you said is correct....
by looking at the code

Integer intvar;

while(...){
intvar=it.next(); //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 intvar=it.next.....
if he typed the code like

while(..){
Integer intvar..

then it is a local variable...

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

Thanks
Kirba.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic