So, basically what I'm creating is a program for Summer Camps which can assign children on an ArrayList into a Cabin that was created. That cabin then is made into a HashMap with the Cabin object as the key and the Camper objects as the elements.
Instead of throwing out pages of code there to have everyone look through what's wrong, I'll just put on pieces of the code with comments of what each object is referencing.
public class SelectCabin extends JFrame {
private JComboBox comboBox;
public SelectCabin() {
super("ASSIGN CABINS");
getContentPane().setForeground(Color.CYAN);
getContentPane().setBackground(new Color(205, 133, 63));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(300, 300, 850, 600);
getContentPane().setLayout(null);
JButton deleteButton = new JButton("SELECT");
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
AssignCampers group = new AssignCampers(); // **Error occurs here
group.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
group.getOrCreateGroup((Cabin)comboBox.getSelectedItem());
//Gets the Cabin from the drop down list, and creates a HashMap with that Cabin as the key.
group.initCampersModel((Cabin)comboBox.getSelectedItem());
//Initializes the model based on the cabin it is selecting, so it only shows campers that meet that cabin criteria. That has not been implemented yet though.
group.setVisible(true);
}
});
deleteButton.setBackground(new Color(255, 215, 0));
deleteButton.setFont(new Font("Tahoma", Font.BOLD, 18));
deleteButton.setBounds(324, 403, 156, 64);
getContentPane().add(deleteButton);
JLabel lblSelectACabin = new JLabel("Select a Cabin to Assign Campers To");
lblSelectACabin.setFont(new Font("Tahoma", Font.BOLD, 18));
lblSelectACabin.setBounds(240, 33, 464, 82);
getContentPane().add(lblSelectACabin);
comboBox=new JComboBox();
comboBox.setBounds(289, 146, 226, 41);
comboBox.setModel(new DefaultComboBoxModel(NewCabin.cabinList.toArray()));
getContentPane().add(comboBox);
}
}
The SelectCabin class basically takes Cabin objects that were created and added to a static ArrayList, puts them in a drop down menu, and allows you to select one.
Once you select one, the event handler opens in the AssignCamper class. This class contains methods which one creates the HashMap, and other makes a JList for campers to add to the cabin. I only created that one JList so far, because the program isn't working thus far, so I'm not continuing to make the same error.
Here is the AssignCamper class.
public class AssignCampers extends JFrame {
private JPanel contentPane;
static Map<Cabin, Set<Camper>> cabinMap= new HashMap<>();
static Map<Cabin, Set<Counselor>> cabinMapCounselor= new HashMap<>();
private DefaultListModel campersModel;
private DefaultListModel campersModelAssigned; //not used yet
private DefaultListModel counselorsModel; //not used yet
private DefaultListModel counselorsModelAssigned; //not used yet
public AssignCampers() {
getContentPane().setForeground(Color.CYAN);
getContentPane().setBackground(Color.GREEN);
getContentPane().setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(300, 300, 850, 700);
getContentPane().setLayout(null);
JScrollPane scrollPaneAddedCampers = new JScrollPane();
JScrollPane scrollPaneCampers= new JScrollPane();
scrollPaneAddedCampers.setBounds(495, 299, 258, 275);
scrollPaneCampers.setBounds(83, 299, 258, 275);
getContentPane().add(scrollPaneAddedCampers);
getContentPane().add(scrollPaneCampers);
JList camperJlist = new JList(campersModel); //**Error Occurs Here
scrollPaneCampers.setViewportView(camperJlist);
JScrollPane scrollPaneCounselors = new JScrollPane();
scrollPaneCounselors.setBounds(83, 154, 258, 100);
getContentPane().add(scrollPaneCounselors);
JScrollPane scrollPaneAddedCounselors = new JScrollPane();
scrollPaneAddedCounselors.setBounds(495, 154, 258, 100);
getContentPane().add(scrollPaneAddedCounselors);
JButton btnAdd = new JButton("-->");
btnAdd.setFont(new Font("Tahoma", Font.BOLD, 20));
btnAdd.setBounds(365, 327, 97, 67);
getContentPane().add(btnAdd);
JButton btnRemove = new JButton("<--");
btnRemove.setFont(new Font("Tahoma", Font.BOLD, 20));
btnRemove.setBounds(365, 445, 97, 67);
getContentPane().add(btnRemove);
JButton btnAddCounselor = new JButton("-->");
btnAddCounselor.setFont(new Font("Tahoma", Font.BOLD, 13));
btnAddCounselor.setBounds(365, 168, 97, 25);
getContentPane().add(btnAddCounselor);
JButton btnRemoveCounselor = new JButton("<--");
btnRemoveCounselor.setFont(new Font("Tahoma", Font.BOLD, 13));
btnRemoveCounselor.setBounds(365, 206, 97, 25);
getContentPane().add(btnRemoveCounselor);
}
public Set<Camper> getOrCreateGroup(Cabin cabin){
return AssignCampers.cabinMap.computeIfAbsent(cabin, (unused)-> new HashSet<>());
}
public void initCampersModel(Cabin cabin){
Collections.sort(NewCamper.camperList2, new Comparator<Camper>(){
public int compare(Camper c1, Camper c2){
return c1.getDob().compareTo(c2.getDob());
}
});
for(Camper c: NewCamper.camperList2){ //static ArrayList
campersModel.addElement(c);
}
}
}
I've tried everything from deleting how the campersModel is sorted, to taking the arguments out of the initCampersModel method, but I keep getting the error that it cannot be non null.
I thought maybe I couldn't use the event handler that way, so I even went as far as making the comboBox static so that I can go into the AssignCamper class and take that value from it to make it work, but I still got the same exact error.
I've used the DefaultListModel and JList to delete campers and delete cabins, and it shows up fine, but for some reason here, I cannot get this JList to work.
Any clues here would be wonderful.
Thanks.
The error is self-explanatory. Your DefaultListModel
objects are still null because you didn't actually create them when you declared them. What you should have done is
private DefaultListModel campersModel = new DefaultListModel();
private DefaultListModel campersModelAssigned = new DefaultListModel();
private DefaultListModel counselorsModel = new DefaultListModel();
private DefaultListModel counselorsModelAssigned = new DefaultListModel();