Hello, this is my first time posting here and probably not my last. I'm currently learning Java at uni and have run into a problem with my code. First I'll give you the code, then explain my problem.
The first part is Class Board, this sets up a board for the board game with an array to show if the board is populated with Black, White or blank spaces.
Code:
import java.awt.*;
import java.awt.event.*;
public class Board extends Frame
{
public int size;
public String[][] pieces;
public int increments;
public Board(int Size)
{
size = Size;
setTitle("The Board");
setSize(520,560);
setBackground(Color.white);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
pieces = new String[size][size];
for (int r=0; r<size; r++)
{
for (int c=0; c<size; c++)
{
pieces[r][c] = " - ";
}
}
increments = (500 / (size));
}
public void paint(Graphics g)
{
/**
* draw grid
*/
for (int i=0; i<size; i++)
{
g.drawLine(10, (40 + (i * increments)), 510, (40 + (i * increments)));
g.drawLine((10 + (i * increments)), 40, (10 + (i * increments)), 540);
}
g.drawLine(10, 540, 510, 540);
g.drawLine(510, 40, 510, 540);
for (int r=0; r<size; r++)
{
for (int c=0; c<size; c++)
{
if (pieces[r][c] == " B ")
{
g.fillOval(((r*increments)+(increments/2)),((c*increments)+(increments/2)),increments-10,increments-10);
} else if (pieces[r][c] == " W ")
{
g.drawOval(r*increments, c*increments, increments-5, increments-5);
}
}
}
}
}
This second class part just creates the Board object with size 8.
Code:
public class Game
{
public static void main(String[] args)
{
Board theBoard = new Board(8);
}
}
My problem isn't a big one, despite the code. I've narrowed it down to these if statements.
if (pieces[r][c] == " B ")
else if (pieces[r][c] == " W ")
These are in public void paint(Graphics g). Despire the pieces[][] array being in the main Board class which I thought was global, these aren't included in the paint() method.
How can I sort this out?
Edit: The error message that comes up is a Java.lang.NullPointerException