Dale Harris
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:
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:
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.
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;
}
}
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);
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.
Show how two pieces of your code actuall are comabined. when you execute the second snippet ?
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
- so when would you have a chance to say this:
Hashtable<String, String> zipCodesListing = ZipCodes.zipCodesList;
cityName = zipCodesListing.get(cityCo de);
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;
}
- so when would you have a chance to say this:
Hashtable<String, String> zipCodesListing = ZipCodes.zipCodesList;
cityName = zipCodesListing.get(cityCo
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(cityCo de);
You shuld do something like that - all from when you push the button
ZipCodes.populate();
Hashtable<String, String> zipCodesListing = ZipCodes.zipCodesList;
cityName = zipCodesListing.get(cityCo
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");
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!
Thanks again!
ASKER
Great, fast, easy solution!