Results 1 to 14 of 14
  1. #1
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    97
    Rep Power
    0

    Default ** SOLVED ** EnumMap, JLists & classes

    I have a program I am creating in Eclipse. In my JFrame window, I am adding a JList object that I want to load with an EnumMap that I have created in a separate 'enum' class.

    In my JFrame, I have a method called 'loadJList()' in which I want to iterate through the EnumMap and load the keys & values.

    Here's what I've tried
    -------------------------------------
    In my EnumMap class, I've done the following ...

    Java Code:
    public enum MyEnumMap {
    .....//created map values & keys, getter() and setter()
    
    public Enum<MyEnumMap> getEnum() {
    		return this;
    	}
    }
    I imported my EnumMap program in my JFrame program and tried something like ...

    Java Code:
    public void loadJList() {
    for(Enum<?> e: myEnumMap.values()) {
    		list.add ....
    	}
    }
    which does not work. I've scoured the internet a bit but sources on EnumMap are limited, at least ones I can understand (first time trying to use this).
    -------------------------------------

    I also need to elaborate on the key of the EnumMap (they are cities, I'd like to add the state or country). For example

    LASVEGAS, 749.99

    I would want displayed as

    Las Vegas, Nevada .... 749.99


    My ultimate goal here is to access my EnumMap in my JFrame program to dynamically load a JList object. Any advice would be appreciated.

    I apologize in advance for my choppy verbiage, just trying to be thorough in my explanation.
    Last edited by mallorz; 09-21-2014 at 09:32 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: EnumMap, JLists & classes

    which does not work.
    Please copy the full text of the error message and paste it here.

    Also paste code that caused it. Be sure to wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    97
    Rep Power
    0

    Default Re: EnumMap, JLists & classes

    Quote Originally Posted by Norm View Post
    Please copy the full text of the error message and paste it here.
    Norm, I haven't gotten anything actually functional. I used this as a reference however e.keyset and the like do not seem to be options.

    Is .values() correct to use in the for loop?

    I may just turn everything around and use a HashMap, it's so much easier.

  4. #4
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    97
    Rep Power
    0

    Default Re: EnumMap, JLists & classes

    Here is some of my code. This is my enum ...

    Java Code:
    public enum DestinationsEnum {
    	
    	// Keys
    	CABO, CANCUN, DUBLIN, SYDNEY, LASVEGAS, LONDON, MADRID, NEWYORK, PARIS, SANFRANCISCO;
    	
    	private String location;
    	
    	public static void main(String[] args) {
    		
    		// Create EnumMap
    		EnumMap<DestinationsEnum, Double> destinations = new EnumMap<DestinationsEnum, Double>(DestinationsEnum.class);
    		
    		// Associate values in map
    		destinations.put(SYDNEY, 949.99);
    		destinations.put(CABO, 1049.99);
    		destinations.put(SANFRANCISCO, 499.99);
    		destinations.put(DUBLIN, 1099.99);
    		destinations.put(CANCUN, 699.99);
    		destinations.put(NEWYORK, 449.99);
    		destinations.put(PARIS, 1499.99);
    		destinations.put(MADRID, 1299.99);
    		destinations.put(LASVEGAS, 349.99);
    		
    	} // End of main
    	
    	public void setLocation(String location) {
    		this.location = location;
    	}
    	
    	public String getLocation() {
    		return location;
    	}
    	
    	public Enum<DestinationsEnum> getEnum() { // Not sure about this
    		return this;
    	}
    	
    } // End of enum
    In my JFrame I have

    Java Code:
    JList<Enum> list = new JList<Enum>();
    
    ....
    
    	public void loadJList() {
    		
    		for(Enum<?> e: DestinationsEnum.values()) { // Everything in here is very wrong - I know this.
    			list.add(e + "....." + e.keys)
    		}
    		
    	} // End of loadJList();

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: EnumMap, JLists & classes

    Take a look at the tutoriaL: Enum Types (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    especially the Planets enum near the bottom.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    97
    Rep Power
    0

    Default Re: EnumMap, JLists & classes

    Quote Originally Posted by Norm View Post
    Take a look at the tutoriaL: Enum Types (The Java„ Tutorials > Learning the Java Language > Classes and Objects)
    especially the Planets enum near the bottom.
    Thanks, Norm. I think I am closer. I'll post back when I am able to test it. For now I am error-free! I appreciate the reply.
    Last edited by mallorz; 09-21-2014 at 02:52 AM.

  7. #7
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    97
    Rep Power
    0

    Default Re: EnumMap, JLists & classes

    Error-free in the design environment, but at runtime I get a 'Null Pointer Exception'. Pretty sure it still has to do with how I am trying to load my JList. I've added a Model for this. See below, I think the enumMap looks better?

    Java Code:
    public enum DestinationsEnum {
    	
    	// Keys
    	CABO ("Cabo San Lucas, Mexico", 1049.99), 
    	CANCUN ("Cancun, Mexio", 699.99), 
    	DUBLIN ("Dublin, Ireland", 1099.99), 
    	SYDNEY ("Sydney, Australia", 949.99), 
    	LASVEGAS ("Las Vegas, Nevada", 349.99), 
    	LONDON ("London, England", 1499.99), 
    	MADRID ("Madrid, Spain", 1299.99), 
    	NEWYORK ("New York, New York", 449.99), 
    	PARIS ("Paris, France", 149.99),
    	SANFRANCISCO ("San Franciso, California", 499.99);
    	
    	private String location;
    	private Double price;
    	
    	DestinationsEnum(String location, Double price) {
    		this.location = location;
    		this.price = price;
    	}
    	
    	public String getLocation() {
    		return location;
    	}
    	
    	public Double getPrice() {
    		return price;
    	}
    	
    	public void setLocation(String location) {
    		this.location = location;
    	}
    	
    } // End of enum

    Back in my JFrame ...
    Java Code:
    public class TrainStation extends JFrame {
    private JList<String> list = new JList<String>();
    private DefaultListModel<String> model;
    // Other objects
    
    public TrainStation() {
    		setModel(); // For JList ... [NullPointerException]
    		initComponents(); // Build GUI
    	} // End of TrainStation() method
    
    ... // In initComponents()
    contentPane.add(list);
    model = new DefaultListModel<String>();
    ....
    
    public void setModel() {
    		
    		for (DestinationsEnum d : DestinationsEnum.values()) {
    			model.addElement(d.getLocation() + " ..... " + d.getPrice()); // [NullPointerException]
    		}
    		
    		list.setModel(model);
    		
    	} // End of setModel()
    }

    Thoughts?

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: EnumMap, JLists & classes

    at runtime I get a 'Null Pointer Exception'.
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    97
    Rep Power
    0

    Default Re: EnumMap, JLists & classes

    Quote Originally Posted by Norm View Post
    Please copy the full text of the error message and paste it here. It has important info about the error.
    Java Code:
    java.lang.NullPointerException
    	at edu.witc.TrainStation.view.TrainStation.setModel(TrainStation.java:106)
    	at edu.witc.TrainStation.view.TrainStation.<init>(TrainStation.java:47)
    	at edu.witc.TrainStation.view.StartProgram$1.run(StartProgram.java:24)
    	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$200(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: EnumMap, JLists & classes

    java.lang.NullPointerException
    at edu.witc.TrainStation.view.TrainStation.setModel(T rainStation.java:106)
    at edu.witc.TrainStation.view.TrainStation.<init>
    Look at line 106 and see what variable has a null value. Use a println() if you can't tell.
    Then backtrack in the code to see why that variable does not have a valid value.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    97
    Rep Power
    0

    Default Re: EnumMap, JLists & classes

    Line 106 was the setModel(). Swapped that to be after the GUI is built and it displays.

    Java Code:
    public TrainStation() {
    		setResizable(false);
    		initComponents(); // Build GUI
    		setModel(); // For JList
    	} // End of TrainStation() method

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: EnumMap, JLists & classes

    Is it working now? Please mark as Solved.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    97
    Rep Power
    0

    Default Re: EnumMap, JLists & classes

    Not to be completely dense but where do I do that?

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: EnumMap, JLists & classes

    Sorry, I've never done it. Maybe in the Go Advanced section.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Two JLists
    By Psygnosys in forum AWT / Swing
    Replies: 2
    Last Post: 04-03-2012, 09:57 PM
  2. JLists
    By loopsnhoops in forum Advanced Java
    Replies: 3
    Last Post: 05-28-2011, 12:22 AM
  3. JLISTS and Classes
    By loopsnhoops in forum New To Java
    Replies: 4
    Last Post: 05-27-2011, 07:54 AM
  4. JLists and JTables
    By loopsnhoops in forum New To Java
    Replies: 2
    Last Post: 05-27-2011, 01:19 AM
  5. Question about JLists
    By Psyclone in forum AWT / Swing
    Replies: 4
    Last Post: 02-21-2010, 03:56 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
  •