繁体   English   中英

为什么返回“ dataModel必须为非null”错误?

[英]Why is this returning a “dataModel must be non null” error?

因此,基本上我要创建的是一个夏令营程序,可以将ArrayList上的孩子分配到已创建的小屋中。 然后将该小屋以Cabin对象作为键,将Camper对象作为元素,制成HashMap。

与其在这里扔掉代码页以使所有人都看穿错误,不如在代码片段上加上每个对象所引用内容的注释。

 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类基本上接受已创建并添加到静态ArrayList的Cabin对象,将它们放在下拉菜单中,并允许您选择一个对象。

选择一个事件处理程序后,该事件处理程序将在AssignCamper类中打开。 此类包含一种方法,一种创建HashMap,另一种创建JList供露营者添加到机舱。 到目前为止,我只创建了一个JList,因为该程序到目前为止还无法正常工作,因此我不会继续犯同样的错误。

这是AssignCamper类。

  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方法中删除参数,但我一直收到这样的错误,即它不能为非null。

我以为也许我不能以这种方式使用事件处理程序,所以我甚至尽可能地使comboBox成为静态的,这样我就可以进入AssignCamper类并从中获取该值以使其起作用,但是我仍然可以完全相同的错误。

我已经使用DefaultListModel和JList删除了露营者并删除了小木屋,它显示得很好,但是由于某种原因,我无法使这个JList正常工作。

这里的任何线索都会很棒。

谢谢。

该错误是不言自明的。 您的DefaultListModel对象仍然为null,因为在声明它们时实际上并未创建它们。 你应该做的是

private DefaultListModel campersModel = new DefaultListModel();
private DefaultListModel campersModelAssigned = new DefaultListModel();
private DefaultListModel counselorsModel = new DefaultListModel();
private DefaultListModel counselorsModelAssigned = new DefaultListModel();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM