-2
private void drawSquare (Graphics g, int row, int col,
        Tetrominoes shape){

    Color[] colors={new Color(0,0,0), new Color(204,102,102),
            new Color(102,204,102), new Color(102,102,204),
            new Color(204,204,102), new Color(204,102,204),
            new Color(102,204,204), new Color(218,170,0)
    };
    int x = col * squareWhidth();
    int y = row * squareHeight();
    Color color = colors[shape.ordinal()];  // **** NullPointerException here ****
    g.setColor(color);
    g.fillRect(x + 1, y + 1, squareWhidth() - 2,
    squareHeight() - 2);
    g.setColor(color.brighter());
    g.drawLine(x, y + squareHeight() - 1, x, y);
    g.drawLine(x, y, x + squareWhidth() - 1, y);
    g.setColor(color.darker());
    g.drawLine(x + 1, y + squareHeight() - 1,
    x + squareWhidth() - 1, y + squareHeight() - 1);
    g.drawLine(x + squareWhidth() - 1,
    y + squareHeight() - 1,
    x + squareWhidth() - 1, y + 1);
}

Throws me a nullpointerexception when I run the program and the terminal reference me in this line: Color color = colors[shape.ordinal()];

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Kevin Lopez
  • 63
  • 1
  • 9

1 Answers1

2

If the NullPointerException incriminates this line: Color color = colors[shape.ordinal()];then it logically implies that your shape object is null at that point. You should debug the call to drawSquare and check what shapeobject the calling method is passing into it.

alainlompo
  • 4,414
  • 4
  • 32
  • 41