我正在寻找一种方法来改变按钮,然后说#34;玩家1结束转弯"到"玩家2结束转弯"按下时以下是我所拥有的:
private int pTurn = 1;
Button turn = new Button();
turn.setText("Player " + pTurn + " End Turn");
turn.setOnAction(new turnButton());
//There is code between these two blocks, but it isn't important for
//this question I don't think
class turnButton implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
pTurn++;
if (pTurn == 3) {
pTurn = 1;
}
turn.setText("Player " + pTurn + " End Turn");
}
}
当我运行此按钮时,我收到以下错误:
Executing /Users/bronsonlane/NetBeansProjects/FinalProject/dist/run1715909217/FinalProject.jar using platform /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre/bin/java
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at halma.Halma$turnButton.handle(Halma.java:234)
at halma.Halma$turnButton.handle(Halma.java:227)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
答案 0 :(得分:-1)
您需要在按钮转动前声明pTurn变量。 pTurn未设置且返回null。
int pTurn = 1;
Button turn = new Button();
turn.setText("Player " + pTurn + " End Turn");
turn.setOnAction(new turnButton());
class turnButton implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
pTurn++;
if (pTurn == 3) {
pTurn = 1;
}
turn.setText("Player " + pTurn + " End Turn");
}
}