Get String from another method

隐身守侯 提交于 2020-01-11 14:36:30

问题


I have 2 methods, the first one where i display selected value, index from my JList(list). What i want to do is to send selectedValue - s - to CreateMap method. I tried this code, but s variable is null. Why?

public void actionPerformed(ActionEvent e)
    {
        int index = 0;

        if(e.getActionCommand().equals("Check")){ //if button is pressed

            index = list.getSelectedIndex();
            System.out.println("Index selected" + index);
            String s = (String) list.getSelectedValue();
            System.out.println("Value Selected " +s);

            createMap();

    }

}


     private Map<String, Integer>createMap()
     {
         Map<String, Integer> graphicsMap = new HashMap<>();

         for(LaneInformation l:graphics.laneInfos )
         {
             if (l.getEllipse().contains(graphics.startX, graphics.startY)) {
                 graphicsMap.put(this.s, graphics.startX);


             }

         }

         return graphicsMap;     
     }

回答1:


I assume you have 2 String variables called s, one global and one local in actionPerformed.

Change the line

String s = (String) list.getSelectedValue();

to

this.s = (String) list.getSelectedValue();

And it should work. Be sure you have a global variable called String s.



来源:https://stackoverflow.com/questions/28961811/get-string-from-another-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!