简体   繁体   中英

Unable to fetch value from ArrayList saved in an object , and object saved in a HashMap

I am working on Swing and I am unable to fetch the value from arrayList saved in an object , which is then saved in a hashMap .

system.out.println prints no values.

My code contains a procedure list, and a parameter list. After clicking on the add parameter button the parameters get added to paramList

When add procedure button is clicked the procedure fetched from textfield is saved in procList .

Values from paramList are saved in new Object (object contains arraylist with getters and setters)

The Hashmap stores the procedure name as key and the object as value

Then, I have another event, where we click on procedure List on GUI, then we fetch the selected procedure name,

That selected procedure name can be used as Key to fetch value from the HashMap. we get the array list of parameters from hashmap using the key.

But when i tried to display in the GUI then it was blank, Then i tried to see if values are there using System.out.println and no value from printed :-(

The issue is with private void getParameterList

here is the code.

//variable declaration
 String procedureName;
 String parameterName;
 ArrayList<String> procList = new ArrayList<>();
 ArrayList<String> paramList = new ArrayList<>();
 HashMap<String,ParamListPojo> procMapper = new HashMap<>();

private void Button_addParameterActionPerformed(java.awt.event.ActionEvent evt) {                                                    
       parameterName = paramField.getText();
       paramList.add(parameterName);
       //System.out.println(paramList);

       DefaultListModel paramListModel = new DefaultListModel();
       //populating DefaultListModel for parameters
        Iterator<String> it2 = paramList.iterator();
        while(it2.hasNext()){
            paramListModel.addElement(it2.next()); 
        }

        //populating parameterList
        parameterList.setModel(paramListModel); 
        paramField.setText(""); 
    }     

private void button_addProcedureActionPerformed(java.awt.event.ActionEvent evt) {                                                    
        //procedureName = level_funcPrefix.getText() +procedureField.getText();
        ParamListPojo paramListPojo = new ParamListPojo();
        procList.add(procedureName);

        //Storing procedures and parameters in MAP
        paramListPojo.setParamList(paramList); 
        procMapper.put(procedureName,paramListPojo);
        System.out.println(procList);
        System.out.println(paramList);
        System.out.println(procMapper);

        procedureField.setText("");

         DefaultListModel procListModel  = new DefaultListModel();
         DefaultListModel paramListModel = new DefaultListModel();

         // populating DefaultListModel for procedureList
         Iterator<String> it1 = procList.iterator();
         while(it1.hasNext()){
             procListModel.addElement(it1.next());
         }

        //populating DefaultListModel for parameters
        Iterator<String> it2 = paramList.iterator();
        while(it2.hasNext()){
            paramListModel.addElement(it2.next()); 
        }

        // populating procedureList 
        procedureList.setModel(procListModel);
        procedureList.setSelectedIndex(procList.size()-1);

        //populating parameterList
        parameterList.setModel(paramListModel); 
        paramList.clear();
    }  

// populate Parameter List on GUI when procedure is selected 
    private void getParameterList(java.awt.event.MouseEvent evt) {                                  

        String procedure = procedureList.getSelectedValue();
        System.out.println("MouseClickEvent , procedure is "+procedure);

        //ParamListPojo prjL = new ParamListPojo();
        ParamListPojo prjL = procMapper.get(procedure);
        paramList = prjL.getParamList();

        System.out.println(paramList);
        //Declaring DefaultListModel for parameters
        DefaultListModel paramListModel = new DefaultListModel();

        Iterator<String> it = paramList.iterator();
        while(it.hasNext()){
            String parameter = it.next();
            paramListModel.addElement(parameter); 
            System.out.println(parameter);
        }

The console output is

[p_csacas]
[scasca, ascacasc, ascacs]
{p_csacas=wrapperbuilder.ParamListPojo@1150594}
[p_csacas, f_ascasc]
[rrrrrr]
{p_csacas=wrapperbuilder.ParamListPojo@1150594, f_ascasc=wrapperbuilder.ParamListPojo@26df23}
MouseClickEvent , procedure is p_csacas
[]

So we can see while adding procedure the values are there in both list, ans also in the HashMAP But while fetching on mouse click on list in GUI , the output is coming blank.

Sample image of GUI is as given GUI的样本图像

Please help me with this :-(

The problem is solved. The issue was with the setter method of arrayList in ParamListPojo class.

public void setParamList(ArrayList<String> procList) {
        //this.prmList = procList;
                prmList.addAll(procList);
    }

previously i was using the this.prmList = procList now i am using prmList.addAll(procList);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM