Results 1 to 5 of 5
  1. #1
    mallorz is online now Member
    Join Date
    Jan 2014
    Posts
    86
    Rep Power
    0

    Default HashMap is giving me a headache!

    Hello. I am relatively new to Java as a whole (currently in my second class), however the concept of HashMaps seems to be giving me issues. It all seems very easy and straight-forward, however I can't for the life of me figure out why I am getting the errors I am. I believe it all is stemming from a ClassCastException on the following:

    Java Code:
    Map<String, Double> priceMap = (Map<String, Double>) new HashMap();
    A bit about what I am trying to do:

    I have a main program which launches a JFrame. In this JFrame is a ComboBox I am trying to populate upon the application loading. I want the objects in the combobox to come from a HashMap I've created in a separate program/class.

    So, in my user interface (the JFrame) I define the following:

    Java Code:
    protected JComboBox<Object> jcbItems = new JComboBox<>(); 
    ....
    public Interface() {
    		
    		loadCombo();
    		initComponents(); // Build GUI
    		
    		@SuppressWarnings("unused")
    		MainController controller = new MainController(this.getView(), map);
    		
    	} // End of Interface() method
    	
    	
    	private void loadCombo() {
    		
                    map.setMap();
    		for (Object key : map.getMap().keySet()) {
    			jcbItems.addItem(key);
    		}
    } // End of loadCombo() method
    ....
    My HashMap class looks like this:
    Java Code:
    public class HashMap {
    
    	private Map<String, Double> priceMap = null;
    	
    	public void setMap() {
    		
    		@SuppressWarnings("unchecked")
    		Map<String, Double> priceMap = (Map<String, Double>) new HashMap(); // Issue??
    		
    		priceMap.put("Ball", 3.98);
    		priceMap.put("Frisbee", 7.98);
    		priceMap.put("Football", 11.98);
    		priceMap.put("Basketball", 14.98);
    		priceMap.put("Glove", 12.99);
    		priceMap.put("Cleats", 39.99);
    		priceMap.put("Socks", 3.99);
    		priceMap.put("Hat", 9.99);
    		
    	} // End of setMap()
    	
    	public Map getMap() {
    		return priceMap;
    	} 
    	
    	public void selectedItem(String item) {
    		getPrice(item);
    	} // End of selectedItem method
    
    
    	private Double getPrice(String item) {
    		Map<?, ?> map = getMap();
    		Double p = 0.00;
    		
    		p = (Double) map.get(item); // Price, cast to Double
    		
    		return p;
    	} // End of getPrice method
    	
    } // End of HashMap class
    I have imported java.util.Map & HashMap in my HashMap class and I've also imported my HashMap class into Interface.

    If anyone could shed some light on this I would greatly appreciate it!

    *** EDIT ***
    I found it - I had my class titled HashMap as well which was conflicting with the import/instance. Renamed my class and now I'm getting a NullPointerException. One step at a time! :)

    Thanks for looking anyway!
    Last edited by mallorz; 09-02-2014 at 12:10 AM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,280
    Rep Power
    6

    Default Re: HashMap is giving me a headache!

    Its not only HashMap you are struggling with, there are many issues in your code. First of all, the reason why you had to resort to adding a typecast is because you are creating the map wrong.

    Java Code:
    Map<String, Double> priceMap = new HashMap<String,Double>();
    Or to save typing since Java 7 you can do this:

    Java Code:
    Map<String, Double> priceMap = new HashMap<>();
    And your code contains another classic mistake: creating duplicates.

    Java Code:
    public class HashMap {
     
        private Map<String, Double> priceMap = null;  // priceMap number one, as a class property
         
        public void setMap() {
             
            @SuppressWarnings("unchecked")
            Map<String, Double> priceMap = (Map<String, Double>) new HashMap(); // priceMap number two, as a local variable
    You only want one priceMap variable. I'll leave it up to you to reason which of the two.


    There are plenty more problems to fix, at this point you have to read your study material again and properly figure it out because I can see you've been trying to get things working by either just trying something or by letting an IDE fix problems for you. I'll give you some hints what to look at.

    Java Code:
    public Map getMap() { // there is something missing here...
            return priceMap;
        }

    Java Code:
    private Double getPrice(String item) { 
            Map<?, ?> map = getMap();  // ... and that is why you got to this really special code
            Double p = 0.00;
             
            p = (Double) map.get(item); // You shouldn't need a typecast here either.
    Good luck.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    mallorz is online now Member
    Join Date
    Jan 2014
    Posts
    86
    Rep Power
    0

    Default Re: HashMap is giving me a headache!

    Thanks for the reply, gimbal. I'm aware the current code is littered with errors. A few of those you mentioned were due to the error I was getting when I originally tried to create the Map instance. Like I said above, and similar to one of the items you mentioned, I had a duplicate name. It's kind of even more of a headache when you get one error that makes you try a bunch of things and mucks up your program further. Haha.

    My approach when it comes to programming is to write it all out and debug on the fly one-by-one when I try to run it.

    Your tips are helpful though. Thank you again!

  4. #4
    mallorz is online now Member
    Join Date
    Jan 2014
    Posts
    86
    Rep Power
    0

    Default Re: HashMap is giving me a headache!

    It's crazy how much one little error can throw you off. I have the program working 98% the way I want now! Just one little quirk to work out.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,280
    Rep Power
    6

    Default Re: HashMap is giving me a headache!

    The lesson being to zoom in on one problem at a time. If you had googled for examples about working with a HashMap in Java, you would have felt a whole lot better about this because you would have gotten to this point all by yourself.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 6
    Last Post: 12-15-2013, 07:40 AM
  2. This loop array stuff is giving me a headache
    By EscSequenceAlpha in forum New To Java
    Replies: 1
    Last Post: 05-08-2012, 10:45 PM
  3. NullPointerException Headache
    By denno in forum New To Java
    Replies: 17
    Last Post: 04-05-2011, 09:13 AM
  4. Modulo Headache
    By House in forum New To Java
    Replies: 17
    Last Post: 09-01-2010, 04:29 AM
  5. Event headache
    By kammenos in forum Java Applets
    Replies: 2
    Last Post: 12-13-2008, 12:59 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •