Solved

Why doesn't my hashtable populate? Java

Posted on 2012-03-20
7
441 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to parse custom JSON to POJO java 4 56
how to add new optional parameter to JSP 1 29
MySQL  on Tomcat 8 30
eclipse apache tomcat admin console 52 66
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
You should read OS supplied guidelines before developing. I can't stress that enough. The guidelines will help you understand the reasons mobile app developers do what they do.  Apple is very particular when they review appstore submissions.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

867 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now