Results 1 to 7 of 7
  1. #1
    koddy is offline Member
    Join Date
    Apr 2010
    Posts
    20
    Rep Power
    0

    Default how to read the ArrayList inside HashMap

    Hallo,

    Ive a problem by reading the ArrayList inside the HashMap.

    I have a class, to fill this HashMap:
    Map<String, MSFlank> flanks = new HashMap<String, MSFlank>();

    This HashMap is going to fill the Map on MSFlank-class.
    On that MSFlank-class, I have another ArrayList:
    private List<MSTooth> mstooths = new ArrayList<MSTooth> ();
    and on that MSTooth-class, I have another ArrayList:
    private List<MSProfile> msprofiles = new ArrayList<MSProfile> ();

    So... what I need right now is, on another class, I need to read the value on MSProfile ListArray.

    So I made a List on that class like this:
    private List<MSProfile> list_y = new ArrayList<MSProfile>();

    And to fill the list, I made a constructor like this:
    list_y = msDataModel.getMSFlank().get(RIGHT).getMSTooth().g et(0).getMSProfile();


    But.... I got the error:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

    Can someone help me, how can I fix it?

    Thank you very much for the help.

  2. #2
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Can you post the full stack trace?

    It looks like msDataModel.getMSFlank().get(RIGHT).getMSTooth().g et(0).getMSProfile() is returning a null pointer somewhere along the line, a stack trace will tell you which get method is returning a null

    berkeleybross

  3. #3
    koddy is offline Member
    Join Date
    Apr 2010
    Posts
    20
    Rep Power
    0

    Default

    Here is the full stack trace
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
            at Reg_Line_DataModel.<init>(Reg_Line_DataModel.java:43)
            at Controller$RegLineListener.actionPerformed(Controller.java:402)
            at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
            at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
            at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
            at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
            at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
            at java.awt.Component.processMouseEvent(Component.java:6216)
            at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
            at java.awt.Component.processEvent(Component.java:5981)
            at java.awt.Container.processEvent(Container.java:2041)
            at java.awt.Component.dispatchEventImpl(Component.java:4583)
            at java.awt.Container.dispatchEventImpl(Container.java:2099)
            at java.awt.Component.dispatchEvent(Component.java:4413)
            at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
            at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4220)
            at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
            at java.awt.Container.dispatchEventImpl(Container.java:2085)
            at java.awt.Window.dispatchEventImpl(Window.java:2475)
            at java.awt.Component.dispatchEvent(Component.java:4413)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    and at Reg_Line_DataModel.<init>(Reg_Line_DataModel.java: 43) is the line that you just said.

    For a better overview, I give the code which I think needed:

    Java Code:
    public class Reg_Line_DataModel {
        private MSDataModel msDataModel;
        int M = 10;             
    
        private List<MSProfile> list_y = new ArrayList<MSProfile>();    
    
        Double[] X = (Double[]) list_y.toArray(new Double[M+1]); 
    
        String RIGHT;
    
        Reg_Line_DataModel(MSDataModel msDataModel) {
            this.msDataModel = msDataModel;
    
            list_y = msDataModel.getMSFlank().get("FLANK: RIGHT").getMSTooth().get(0).getMSProfile();
    }
    }
    Java Code:
    public class MSDataModel {
        private  String flank;
    
        private Map<String, MSFlank> flanks = new HashMap<String, MSFlank>();
    
        private MSPoints mspoints;
        private MSProfile msProfile;
    
        public MSDataModel() {
        }
    
        public Map<String, MSFlank> getMSFlank(){
            return flanks;
        }
    
        public void addMSFlank(MSFlank f, String flank){
            flanks.put(flank, f);
        }
    Java Code:
    public class MSFlank {
        private String flank;
        private List<MSTooth> mstooths = new ArrayList<MSTooth> ();
    
        public MSFlank(String flank){
            this.flank = flank;
        }
    
        public void setFlank(String str){
            flank = str;
        }
    
        private String getFlank() {
            return flank;
        }
    
        public List<MSTooth> getMSTooth(){
            return mstooths;
        }
    
        public void addMSTooth(MSTooth msTooth){
            this.mstooths.add(msTooth);
        }
    Java Code:
    public class MSProfile {
    
        private String profileID;
        private int points;
        private List<MSPoints> mspoints = new ArrayList<MSPoints> ();
    
        public MSProfile(){
        }
    
        public MSProfile(String profileID){
            this.profileID = profileID;
        }
    
        public void setPoints(int value){
            points = value;
        }
    
        private int getPoints() {
            return points;
        }
    
        public List<MSPoints> getMSPoints(){
            return mspoints;
        }
    
        public void addMSPoints(MSPoints s){
            mspoints.add(s);
        }
    
        public String getProfileID(){
            return profileID;
        }
    
        public void setProfileID(String profileID){
            this.profileID = profileID;
        }
    well... I just crop the code, which I think unneeded here.
    Hope with the code, you can get a better overview.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by koddy View Post
    Java Code:
    list_y = msDataModel.getMSFlank().get(RIGHT).getMSTooth().get(0).getMSProfile();
    Suppose you get this code working, it still drills deep through objects (of another class), through other objects to reach yet another object. May I suggest to read about "The Law Of Demeter" to reduce this deep dependency. Google is your friend here.

    kind regards,

    Jos

  5. #5
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Hmm the stack trace didnt help at all.

    Have you initialised msDataModel? Make sure by doing a System.out.println of it.

  6. #6
    koddy is offline Member
    Join Date
    Apr 2010
    Posts
    20
    Rep Power
    0

    Default

    yo.... the msDataModel is initialised.
    The problem is still
    list_y = msDataModel.getMSFlank().get(RIGHT).getMSTooth().g et(0).getMSProfile();
    Im sure the msDataModel.getMSFlank() is the right one, but after that, Im not sure, how to write the code, since I need to get the list inside another class there.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    list_y = msDataModel.getMSFlank().get(RIGHT).getMSTooth().g et(0).getMSProfile();
    With this long chained statement you are assuming that everything is working the way you want it to. Obviously something is not what you want.
    To find which get..() method is returning a null, break the long statement up into single statements and test the returned value for each call to get...() if its null.

Similar Threads

  1. Read txt file to arrayList
    By koddy in forum New To Java
    Replies: 14
    Last Post: 04-29-2010, 05:15 PM
  2. I can't read a text file located inside my jar.
    By Serrano0811 in forum New To Java
    Replies: 1
    Last Post: 04-21-2010, 05:03 PM
  3. 2D array inside a HashMap
    By darkblue24 in forum New To Java
    Replies: 1
    Last Post: 03-23-2010, 12:39 AM
  4. Replies: 2
    Last Post: 11-29-2009, 10:26 AM
  5. ArrayList into hashMap
    By Preethi in forum New To Java
    Replies: 2
    Last Post: 02-11-2008, 08:13 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
  •