Link to home
Start Free TrialLog in
Avatar of Dale Harris
Dale HarrisFlag for United States of America

asked on

Why doesn't my hashtable populate? Java

I'm trying to pre-populate a Hashtable from a different class called ZipCodes

Here's my ZipCodes class with some test data:

import java.util.Hashtable;

public class ZipCodes {
	final static Hashtable<String, String> zipCodesList
    = new Hashtable<String, String>();
	public static void main() {

		zipCodesList.put("00501", "NY218");
		zipCodesList.put("00544", "NY218");
		zipCodesList.put("00601", "XX499");
		zipCodesList.put("31036", "GA076");
		zipCodesList.put("31037", "ZZ560");
		zipCodesList.put("31038", "ZZ740");
	        return;
	}

}

Open in new window


When I call upon ZipCodes in my code, it compiles fine, it just doesn't have data inside of it.

Here's what my call looks like:

Hashtable<String, String> zipCodesListing = ZipCodes.zipCodesList;
    	      cityName = zipCodesListing.get(cityCode);

Open in new window


The actual part where it does a .get(cityCode) works when the hashtable is being declared and populated in my main code.  But all it's returning is null.
Avatar of for_yan
for_yan
Flag of United States of America image

Show how two pieces of your code actuall are comabined. when you execute the second snippet ?
Avatar of Dale Harris

ASKER

Yes.  It's based on a button push.  The class that I posted called ZipCodes is a standalone class in it's entirety.  That's all it does.  All the rest of my code is in my main java file.
This part will just execute, ppulate Hashatble and then exit

public static void main() {

		zipCodesList.put("00501", "NY218");
		zipCodesList.put("00544", "NY218");
		zipCodesList.put("00601", "XX499");
		zipCodesList.put("31036", "GA076");
		zipCodesList.put("31037", "ZZ560");
		zipCodesList.put("31038", "ZZ740");
	        return;
	}

Open in new window


- so when would you have a chance to say this:

Hashtable<String, String> zipCodesListing = ZipCodes.zipCodesList;
                cityName = zipCodesListing.get(cityCode);
No you can't do that - this class will execute and go away together with your Hashtable


You shuld do something like that - all from when you push the button
ZipCodes.populate();
Hashtable<String, String> zipCodesListing = ZipCodes.zipCodesList;
                cityName = zipCodesListing.get(cityCode);




public static void populate() {

		zipCodesList.put("00501", "NY218");
		zipCodesList.put("00544", "NY218");
		zipCodesList.put("00601", "XX499");
		zipCodesList.put("31036", "GA076");
		zipCodesList.put("31037", "ZZ560");
		zipCodesList.put("31038", "ZZ740");
	        
	}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That was it!  Thanks.  Now I understand what you mean.  It's creating the variable because it's above the method.  But it wasn't doing the rest of it because it was not being called.

Thanks again!
Great, fast, easy solution!