In the following code, if jcbValue evaluates to Find, I have it set to print the contents of the hashmap. It is returning NULL. I am assuming this means that by that point the contents of the hashmap have been emptied.
My questions are as follows;
1) What am I doing wrong?
2) How do I fix it?
Thanks,
CJ
public class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
HashMap<String, ArrayList> map = new HashMap<String, ArrayList>();
String jcbValue = (String) jcbIDF.getSelectedItem();
if (jcbValue == "Insert") {
String Id = jtfId.getText();
ArrayList<String> ValueList = new ArrayList<String>();
String Name = jtfName.getText();
String GPA = jtfGPA.getText();
ValueList.add(Name);
ValueList.add(GPA);
map.put(Id, ValueList);
System.out.println(map);
JOptionPane.showMessageDialog(null, "Record Inserted",
"Result", JOptionPane.INFORMATION_MESSAGE);
// jtfId.setText("");
// jtfName.setText("");
// jtfGPA.setText("");
} else if (jcbValue == "Delete") {
System.out.println(map);
JOptionPane.showMessageDialog(null,
"Delete Selected; But Not Implemented", "Result",
JOptionPane.INFORMATION_MESSAGE);
} else if (jcbValue == "Find") {
System.out.println(map);
JOptionPane.showMessageDialog(null,
"Find Selected; But not Implemented", "Result",
JOptionPane.INFORMATION_MESSAGE);
}
}// Terminates actionPerformed Class
}// Terminates ButtonListenerClass
You’re creating a new map every time you hit actionPerformed, so nothing will ever persist across calls to it.
The map needs to exist as something other than a local variable in the action handler.
(And as AVD says, your comparison is incorrect.)