import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.Console;
import java.util.Scanner;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class CA extends JComponent{
    int[] cells;
    int[] ruleset;
    int w = 10;
    int generation = 0;
    int width = 600; //Pixel length of the window
    private BufferedImage caImage;
    CA()
    {       
        cells = new int[width/w];
        //Rule 90 of Wolfram
        ruleset = new int[]{0,1,0,1,1,0,1,0};
        for (int i = 0; i < cells.length; i++) 
            cells[i] = 0;
        cells[cells.length/2] = 1;
        print(cells, cells.length);
        generate();
    }
    public void generate()
    {
        int [] nextgen = new int[cells.length];
        while(generation<6)
        {
            for(int i=1; i<cells.length-1; i++)
            {
                int left = cells[i-1];
                int middle = cells[i];
                int right = cells[i+1];
                nextgen[i] = rules(left, middle, right);
            }
            cells = nextgen;
            print(cells, cells.length);
            System.out.println();
            paintComponent(this.getGraphics());
            generation++;
        }
    }
    public void print(int[] a, int length)
    {
        System.out.println();
        for(int i=0; i<length; i++)
        {
            System.out.print(a[i]+" ");
        }
    }
    public int rules(int a, int b, int c)
    {
        String s = ""+a+b+c;
        int index = Integer.parseInt(s, 2);
        return ruleset[index];
    }
    public void paintComponent(Graphics g)
    {       
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        for(int i=0; i<cells.length; i++)
        {
            if(cells[i] == 0)       
            {
                Color color = Color.WHITE;
                g2.setColor(color);
            }
            else if(cells[i] == 1)
            {
                Color color = Color.BLACK;
                g2.setColor(color);
            }
            //Rectangle rect = new Rectangle(i*w, generation*2, w, w);
            //g2.fill(rect);
            g2.drawRect(i*w, generation*2, w, w);
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFrame frame = new JFrame();
        frame.setSize(600, 600);
        frame.setTitle("Cellular Automaton");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBackground(Color.RED);
        frame.setVisible(true);
        //Scanner input = new Scanner(System.in);
        //String pause = input.nextLine(); 
        CA cellularAutomaton = new CA();
        frame.add(cellularAutomaton);
        frame.setVisible(true);
    }
}
60 要素の 1D 配列から 1 または 0 を取得して、10x10 ピクセルの白または黒の長方形を表示したいと考えています。1D 配列の更新されたさまざまな要素に対して、同じ操作を 6 回繰り返します。(セルオートマトンの実装)。
問題: この問題は、結果を 600x600 のウィンドウに表示したいときに発生します。行は.g2.setColor(Color)をスローしNullPointerExceptionます。このエラーが発生する理由を見つけることができませんでした。