初始化对象数组网格时出现空点错误

I am getting a null point error when initializing an array grid of objects

我正在尝试初始化一个对象数组网格,但在这样做时出现空点错误。它应该在循环的每次迭代中调用 Cell 中的对象构造函数。相反,我收到了错误。请帮忙

public class Spread extends JPanel {
    private JPanel outputArea;
    private JTextArea numberDead;
    private JTextArea totalInfected;
    private JTextArea newInfections;
    private Integer totalDead;
    private Integer total;
    private Integer newInfected;
    private Integer length = 60;
    private Integer width = 100;
    private Cell[][] label;
    
    public void makeOutputArea() {
        int a;
        int b;
        outputArea = new JPanel(new GridLayout(length, width, -1, -1));
        outputArea.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));

        for (a = 0; a < (length - 1); a++){
            for (b = 0; b < (width - 1); b++)
                label[a][b] = new Cell(); //nullpointerror is located here.
                label[a][b].setPreferredSize(new Dimension(10, 10));
                label[a][b].setOpaque(true);
                outputArea.add(label[a][b]);
                outputArea.setVisible(true);
                if (label[a][b].getInfected() == true) ++total;
            }
        }
    
    public JPanel getOutputArea() {
        return outputArea;
    }
...Additional code follows.

这是应该在数组中初始化的 Cell class。如您所见,它有一个构造函数,每个循环都应调用该构造函数。相反,它给出一个空指针并且从不初始化每个单元格对象。

import java.awt.Color;
import java.util.Random;
import javax.swing.JLabel;

@SuppressWarnings("serial")
public class Cell extends JLabel{
    private int iteration;
    private boolean infected;
    private boolean dead;
    private boolean immune;
    private Random generator = new Random(); //Random number Generator
    private static int infectionRate = 5;
    private static int deathRate = 3;
    
    
    public void setIteration(int i) {
        iteration = i;
    }
    
    public int getIteration() {
        return iteration;
    }
    
    public void setInfected(boolean b) {
        infected = b;
    }
    
    public boolean getInfected() {
        return infected;
    }
    
    public void setDead(boolean a) {
        dead = a;
    }
    
    public boolean getDead() {
        return dead;
    }
    
    public void setImmune(boolean i) {
        immune = i;
    }
    
    public boolean getImmune() {
        return immune;
    }
    
    public Cell() {
        setImmune(false);
        setDead(false);
        setIteration(0);
        if (generator.nextInt(100) < infectionRate) {
            setInfected(true);
            setIteration(++iteration);
        }
        else setInfected(false);
        color();
    }
    
    public void color() {
        if (getInfected() == true) setBackground(Color.RED);
        else if (getDead() == true) setBackground(Color.BLACK);
        else setBackground(Color.GREEN);
    }
    
    public void updateInfection(boolean b) {
        try {
            if (getImmune() == true) return;
            else if (getDead() == true) return;
            else if (getInfected() == true) {
                setIteration(++iteration);
                if (iteration >=4) {
                    if (generator.nextInt(100) < deathRate) {
                        setInfected(false);
                        setDead(true);
                        return;
                    }
                    else {
                        setInfected(false);
                        setImmune(true);
                        return;
                    }
                }
                return;
            }
            else {
                if (generator.nextInt(100) <= 75) {
                    setInfected(true);
                    setIteration(++iteration);
                    return;
                }
                else return;
            }
        }
        finally {
            this.color();
        }
    }
}

Cell [][] label是java中的二维数组。 所以你得先给它分配内存。

只有这样你才能执行如下代码:

label[a][b] = new Cell();

这是关于如何在 java 中正确地将内存分配给二维数组的大量信息,快速谷歌搜索显示 this tutorial