Search code examples
javaarraysgrid-layout

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


I am trying to initialize an array grid of objects and get a null point error when I do so. It should be calling the object Constructor in Cell each iteration of the loop. Instead I am getting the error. Please help.

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.

This is the Cell class it should be initializing in the array. As you can see it has a constructor, which should be called for each loop. Instead it is giving a null pointer and never initializing each cell object.

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();
        }
    }
}

Solution

  • Cell [][] label is a two-dimentional array in java. So you have to allocate the memory for it first.

    Only then you can execute code like:

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

    This is a lot of information about how to correctly allocate memory to 2-dimensional arrays in java, quick googling reveals this tutorial