• Post Reply Bookmark Topic Watch Topic
  • New Topic

I don't know what's wrong with this chess game  RSS feed

steve tang
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry if the post is inetiquette because this my first time using the forum.
I am making a 2 player chess game using JButtons. The program is supposed to run as the following:

1. create the chessboard with pieces in it

2. select a button

3. determine where the piece could move and count the amount of legal moves and record the positions

4. color these buttons

5. goes to step 2

you click the button to move between 5 and 6

6. according to the amount and the positions~to determine is you click is a legal move or not

you click the button to move between 5 and 6

7. switch the information of the 2 buttons you clicked before

8. fresh~output the result of the switch

9. switch turns

When a piece is clicked on, the possible legal moves are shown but when the square is clicked on, nothing happens. It is supposed to move the piece to the selected square. I think the problem is that the program cannot get to step 2 to move the pieces. Also, when one of the pieces is clicked on, the following error message appears that I don't understand:

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at Chess.select(Chess.java:957)
at Chess.movePawn(Chess.java:379)
at Chess.actionPerformed(Chess.java:126)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:18
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.jav
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)

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

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

The code is:
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, and welcome to the Javaranch.

You need to focus on these lines:

at Chess.select(Chess.java:957)
at Chess.movePawn(Chess.java:379)
at Chess.actionPerformed(Chess.java:126)

Your getting a nullpointer exception - in other words, your trying to call
a method, draw ect. something that isn't initialised. Take a look at line
957,379 and 126 in your Chess class. You might wan't to try to "System.out.println("do I exist in memory? "+ object)" to find out if some of your objects doesn't exist.

Feel free to reply if your still having problems.. but please don't make me read 1000 lines of code
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!