Beginner's trouble: Null Point Exception

Hello! New poster here, I'm trying to code a simplified version of Go. For those of you not familiar with the game, the premise is to surround your opponent's pieces with your own to gain territory. Whosoever has the most territory at the end of the game wins.

Basically, my program creates a frame that has a mouse listener object. When the listener "hears" a click, it will call a batch of methods to add a new stone, determine captured stones, remove captured stone, etc. But while running, the program throws a Null Pointer Exception whenever it tries to add a stone to a board (the board is a 2D array)

Here is the beginning of the code for my frame:

public class GoFrame extends JFrame

{

public GoFrame frame;

public GoFrame()

{

final JFrame frame = new JFrame();

final Goban theBoard = new Goban();

final Game theGame = new Game();

theGame.setGameState(true);

theGame.setCurrentTurn("white");

frame.add(theBoard);

frame.add(theGame);

final BoardComponent boardComp = new BoardComponent();

add(boardComp, BorderLayout.CENTER);

class GoListener implements MouseListener

{

public void mousePressed(MouseEvent event)

{

frame.repaint();

add(boardComp, BorderLayout.CENTER);

int x = event.getX();

int y = event.getY();

x = x/20;

y = y/20;

if (x <= 20 && y <= 20)

{

if (theGame.getGameState() == true)

{

Stone o = new Stone(x, y);

o.setColor(theGame.getTurnColor());

theBoard.addStone(o, x, y);

...

The constructor for the Stone.

public class Stone

{

private String color;

private int[] position;

boolean captured;

public Stone(int x, int y)

{

int[] position = new int[2];

position[0] = x;

position[1] = y;

String color = "colorless";

boolean captured = false;

}

And the main method.

public static void main(String[] args)

{

GoFrame lol = new GoFrame();

lol.setTitle("Go");

lol.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final int FRAME_WIDTH = 410;

final int FRAME_HEIGHT = 435;

lol.setSize(FRAME_WIDTH, FRAME_HEIGHT);

lol.setVisible(true);

}

I'm unsure of whether I initializedtheBoard andtheGame in the right place so I initialized them in theGoFrame, which also contains the listener object, thinking that the listener will have access to them. Apparently, the listener cant find those two objects.

Can anyone help?

[2669 byte] By [Kousetsua] at [2007-11-27 6:23:53]
# 1
I suggest that you show the error stacktrace.
eadelarosaa at 2007-7-12 17:42:11 > top of Java-index,Java Essentials,New To Java...
# 2

If you look at the stack trace it tells you which line number it was trying to execute when the exception was thrown. I have a feeling it happens on the frame.repaint() line inside the mouse listener.

You have a class-level GoFrame called "frame" and a local JFrame called "frame", and the wrong one is being referenced. I suggest picking different names, then this problem would have been easier to spot.

Also don't forget to use code tags by highlighting the code and clicking the 'code' button in the post editor.

BinaryDigita at 2007-7-12 17:42:11 > top of Java-index,Java Essentials,New To Java...
# 3

Whatever you think could help:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at Goban.addStone(Goban.java:36)

at GoFrame$1GoListener.mousePressed(GoFrame.java:60)

at java.awt.Component.processMouseEvent(Component.java:6035)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)

at java.awt.Component.processEvent(Component.java:5803)

at java.awt.Container.processEvent(Container.java:2058)

at java.awt.Component.dispatchEventImpl(Component.java:4410)

at java.awt.Container.dispatchEventImpl(Container.java:2116)

at java.awt.Component.dispatchEvent(Component.java:4240)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3983)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)

at java.awt.Container.dispatchEventImpl(Container.java:2102)

at java.awt.Window.dispatchEventImpl(Window.java:2429)

at java.awt.Component.dispatchEvent(Component.java:4240)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)

at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

Kousetsua at 2007-7-12 17:42:11 > top of Java-index,Java Essentials,New To Java...
# 4
at Goban.addStone(Goban.java:36)Check line 36 in your Goban class, in the addStone method. The exact line of code is not part of your original post so I can't be more specific.
BinaryDigita at 2007-7-12 17:42:11 > top of Java-index,Java Essentials,New To Java...
# 5

public void addStone(Stone newStone, int x, int y)

{

GoBoard[x][y] = newStone;

}

Line 36 is GoBoard[x][y] = newStone;

I can't identify the problem.

Here is the relevant portion of the class:

public class Goban extends JComponent

{

private static int s;

private static Stone GoBoard[][];

private boolean capping;

//Contructor

public Goban()

{

int s = 20;

Stone[][] GoBoard = new Stone[s][s];

}

//Returns height/width

public static int getBoardSize()

{

return s;

}

//Returns the array of stones

public static Stone[][] getStones()

{

return GoBoard;

}

//Adds a stone to the array of stones

public void addStone(Stone newStone, int x, int y)

{

GoBoard[x][y] = newStone;

}

Kousetsua at 2007-7-12 17:42:11 > top of Java-index,Java Essentials,New To Java...
# 6

I see. You have a class level array:

private static Stone GoBoard[][];

But your constructor initializes a local array with the same name:

public Goban()

{

int s = 20;

Stone[][] GoBoard = new Stone[s][s];

// GoBoard = new Stone[s][s]; (Should be this!)

}

And so your class level array is never initialized. Note that, for the same reason, your static int s is not set to 20, only the local one is set.

BinaryDigita at 2007-7-12 17:42:11 > top of Java-index,Java Essentials,New To Java...
# 7
I'll begin altering the rest of the code to fit the changes.Thank you very much!
Kousetsua at 2007-7-12 17:42:11 > top of Java-index,Java Essentials,New To Java...
# 8
You're welcome!
BinaryDigita at 2007-7-12 17:42:11 > top of Java-index,Java Essentials,New To Java...