&引用;AWT-EventQueue-0“;java.lang.NullPointerException,ActionListener

&引用;AWT-EventQueue-0“;java.lang.NullPointerException,ActionListener,nullpointerexception,Nullpointerexception,我有以下代码,并在这一行获得NullPointerException: Component storeAll[]=checkPanel.getComponents(); 守则: public class Scroll extends JFrame implements ActionListener { private HashMap<JCheckBox, ArrayList<Integer>> map = new HashMap<>(); JPan

我有以下代码,并在这一行获得NullPointerException:

Component storeAll[]=checkPanel.getComponents(); 
守则:

public class Scroll extends JFrame implements ActionListener  {  

private HashMap<JCheckBox, ArrayList<Integer>> map = new HashMap<>();
JPanel checkPanel;

private static int MAX_CHECKS = 100;

public Scroll() {
    super();
    setLayout(new BorderLayout());

    JPanel checkPanel = new JPanel(new GridLayout(0, 1));
    checkPanel.setLayout(new FlowLayout()); 

    JCheckBox checkBox;
    Random r = new Random();

    for (int i = 0; i < MAX_CHECKS; i++) {
        StringBuilder sb = new StringBuilder();
        ArrayList<Integer> a = new ArrayList<>();
        for (int j = 0; j < 2; j++) {
            Integer temp = (r.nextInt()) % 100;
            a.add(temp);
            sb.append(temp).append(" ");
        }

        checkBox = new JCheckBox(sb.toString().trim());
        checkBox.setName("CheckBox" + i);

        map.put(checkBox, a);
        checkPanel.add(checkBox);
    }

    add(checkPanel,BorderLayout.CENTER); 
    JButton startButton = new JButton("Start");
     add(startButton, BorderLayout.SOUTH);
     startButton.addActionListener(this); 

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
     setSize(800, 800); 
     setVisible(true);


}

        public void actionPerformed(ActionEvent event) {

             Component storeAll[]=checkPanel.getComponents(); 

                for(Component c:storeAll){
                      if(((JCheckBox)c).isSelected())  
                 {  
                  checkPanel.remove(c);  
                  checkPanel.revalidate();  
                  validate();  
                  repaint();  
                 }  

                 else  
                 {  
                  JOptionPane.showMessageDialog(this,"No button to remove");  
                 }  
                }
        }

        public static void main(String[] args) {   

            Scroll asds = new Scroll();
             }   
    }
公共类滚动扩展JFrame实现ActionListener{
private HashMap map=new HashMap();
JPanel检查小组;
私有静态int MAX_CHECKS=100;
公共卷轴(){
超级();
setLayout(新的BorderLayout());
JPanel checkPanel=newjpanel(newgridlayout(0,1));
checkPanel.setLayout(新的FlowLayout());
JCheckBox复选框;
随机r=新随机();
对于(int i=0;i
我以前没有使用过JPanel,但您是否尝试过设置断点,以确保checkPanel在JPanel checkPanel=new JPanel(new GridLayout(0,1));处初始化;?谢谢你,卢克。我以前就把这个问题整理好了。我在scroll()方法之前启动了JPanel,这就是它返回null的原因。谢谢你。