为什么会返回 "dataModel must be non null" 错误?
Why is this returning a "dataModel must be non null" error?
所以,基本上我正在创建的是一个夏令营程序,它可以将 ArrayList 上的 children 分配到创建的 Cabin 中。然后将该小屋制成一个 HashMap,其中 Cabin object 作为键,Camper objects 作为元素。
我不会在那里扔掉几页代码让每个人都看看哪里出了问题,我只是把代码放在上面,并注释每个 object 所引用的内容。
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);
}
}
SelectCabin class 基本上采用创建并添加到静态 ArrayList 的 Cabin object,将它们放在下拉菜单中,并允许您 select 一个。
一旦你 select 一个,事件处理程序就会在 AssignCamper class 中打开。这个 class 包含一个创建 HashMap 的方法,另一个创建 JList 供露营者添加到小屋中。到目前为止,我只创建了一个 JList,因为该程序目前无法运行,所以我不会继续犯同样的错误。
这是 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);
}
}
}
我尝试了所有方法,从删除 campersModel 的排序方式到从 initCampersModel 方法中取出参数,但我一直收到它不能为非空的错误。
我想也许我不能那样使用事件处理程序,所以我什至将组合框设为静态,这样我就可以进入 AssignCamper class 并从中获取该值让它工作,但我仍然得到同样的错误。
我已经使用 DefaultListModel 和 JList 删除露营者和删除小屋,它显示正常,但出于某种原因,我无法让这个 JList 工作。
这里有任何线索都很棒。
谢谢。
错误不言自明。您的 DefaultListModel
对象仍然为空,因为您在声明它们时并未实际创建它们。你应该做的是
private DefaultListModel campersModel = new DefaultListModel();
private DefaultListModel campersModelAssigned = new DefaultListModel();
private DefaultListModel counselorsModel = new DefaultListModel();
private DefaultListModel counselorsModelAssigned = new DefaultListModel();
所以,基本上我正在创建的是一个夏令营程序,它可以将 ArrayList 上的 children 分配到创建的 Cabin 中。然后将该小屋制成一个 HashMap,其中 Cabin object 作为键,Camper objects 作为元素。
我不会在那里扔掉几页代码让每个人都看看哪里出了问题,我只是把代码放在上面,并注释每个 object 所引用的内容。
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);
}
}
SelectCabin class 基本上采用创建并添加到静态 ArrayList 的 Cabin object,将它们放在下拉菜单中,并允许您 select 一个。
一旦你 select 一个,事件处理程序就会在 AssignCamper class 中打开。这个 class 包含一个创建 HashMap 的方法,另一个创建 JList 供露营者添加到小屋中。到目前为止,我只创建了一个 JList,因为该程序目前无法运行,所以我不会继续犯同样的错误。
这是 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);
}
}
}
我尝试了所有方法,从删除 campersModel 的排序方式到从 initCampersModel 方法中取出参数,但我一直收到它不能为非空的错误。
我想也许我不能那样使用事件处理程序,所以我什至将组合框设为静态,这样我就可以进入 AssignCamper class 并从中获取该值让它工作,但我仍然得到同样的错误。
我已经使用 DefaultListModel 和 JList 删除露营者和删除小屋,它显示正常,但出于某种原因,我无法让这个 JList 工作。
这里有任何线索都很棒。
谢谢。
错误不言自明。您的 DefaultListModel
对象仍然为空,因为您在声明它们时并未实际创建它们。你应该做的是
private DefaultListModel campersModel = new DefaultListModel();
private DefaultListModel campersModelAssigned = new DefaultListModel();
private DefaultListModel counselorsModel = new DefaultListModel();
private DefaultListModel counselorsModelAssigned = new DefaultListModel();