Solved

Why doesn't my hashtable populate? Java

Posted on 2012-03-20
7
450 Views
Last Modified: 2012-03-20
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.
0
Comment
Question by:Dale Harris
  • 4
  • 3
7 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37745368
Show how two pieces of your code actuall are comabined. when you execute the second snippet ?
0
 
LVL 16

Author Comment

by:Dale Harris
ID: 37745371
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.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37745372
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);
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 47

Expert Comment

by:for_yan
ID: 37745375
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

0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 37745378
That should ber your class:

import java.util.Hashtable;

public class ZipCodes {
	final static Hashtable<String, String> zipCodesList
    = new Hashtable<String, String>();
	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



and thats how it acn be used in another class:

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

Open in new window

0
 
LVL 16

Author Comment

by:Dale Harris
ID: 37745387
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!
0
 
LVL 16

Author Closing Comment

by:Dale Harris
ID: 37745388
Great, fast, easy solution!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Often, people trade privacy and security for convenience. However in today's concrete jungle, this is an extremely foolish decision considering the vast amount of technologies being used against consumer interest. First off, I won't waste any time e…
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question