title: Get the selected values from JList in Java
body: <p>How can i get the selected value from Jlist?
  I tried the following code, but all variables are displayed null.
  Why index variable is null?</p>
  
  <pre><code> public class GetSelectedValueFromJList extends JFrame implements ActionListener {
      private JList list;
      private JButton checkButton;
  
      public GetSelectedValueFromJList() {
  
  
  
          String[] nameList = { "Value 1", "Value 2", "Value 3", "Value 4", "Value 5"};
          list = new JList(data);
          checkButton = new Button("Check");
          button.addActionListener(this);
  
          //add list to frame
          add(list);
          add(checkButton);
  
      }
  
      public void actionPerformed(ActionEvent e)
      {
          if(e.getCommand().equals("Check"))
          {
              int index = list.getSelectedIndex();
              System.out.println("Index Selected: " + index);
              String s = (String) list.getSelectedValue();
              System.out.println("Value Selected: " + s);
          }
      }
  </code></pre>