Solved

Why doesn't my hashtable populate? Java

Posted on 2012-03-20
7
454 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why doesn't this text field show up on my Applet frame? 2 28
Java 8 to Java 6 8 33
Cisco Anyconnect for Android 6 43
Manage Mobile Phones (devices) 6 61
This is an explanation of a simple data model to help parse a JSON feed
A short article about problems I had with the new location API and permissions in Marshmallow
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 teaches viewers about errors in exception handling.

733 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