The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
October 2001



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


String initialisation in local variables

Posted by Jody Brown on November 01, 2001 at 7:52 AM


I understand what is happening now. Local variables arent assigned a default value when they are declared. So the line:

String nextLocName;

will throw the error you are seeing at compile time. It has not been initialised, not even to null.

If you changed it to:

String nextLocName = null;

it would pass at compile time. If you tried to use it though it will throw a NullPointerException because while it has been initialised, it still has no object reference. This isnt a problem for you though, because you are checking for the null status of the string after the declaration.

I hope this helps.


  • Thnx Hiran November 07, 2001 at 4:31 PM (0)

Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us