I am trying to make a button move to another place, i used this code:
public void actionPerformed(ActionEvent e)
{
for(int i = 0; i<=29; i++)
{
if(e.getSource() == gB[i])
{
System.out.println(i);
}
}
}
and this is draw board
public void drawBoard()
{
JPanel gboard = new JPanel();
for(int i = 0; i < 30; i++)
{
gboard.add(gB[i]);
gB[i].setText(Integer.toString(gB[i].getPieceValue()));
}
which I think is wrong. Anyone have any idea? I’m not able to get the players moving to the next position.
Thanks
the error is: (all in RED)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Controller.actionPerformed(Controller.java:22)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
has only a few names which could hold a value. The
ActionEventnamed “e” and the array named “gB” are the only names which could be set to null.That means either you threw a custom ActionEvent without defining a source, or you didn’t initalize the array gB.
If you wanted your piece to move, it would probably also help to do more than printout the value of “i” in the action handler; but, I’m guessing you haven’t made it that far in your effort yet.