我正在尝试制作一个基本游戏,其中点在屏幕上随机移动,当点击它时,用户获得一些点数。当我运行程序时,我在start方法中得到一个NullPointerException。我想知道我做错了什么以及改进程序的任何类型。谢谢!
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Popup;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
public class CatchTheDot extends Application{
//create ball for game
public static Circle dot;
//create pane to run game
public static Pane window;
//create score counter
int score = 0;
//creat random
Random random = new Random();
@Override
public void start(Stage primaryStage) throws Exception {
window.getChildren().add(dot);
// create scene and place on pane
Scene s = new Scene(window, 800, 800);
primaryStage.setTitle("Catch The Dot");
primaryStage.setScene(s);
primaryStage.show();
//move dot
Timer timer = new Timer();
timer.schedule(new TimerTask()
{
@Override
public void run(){
window.getChildren().remove(dot);
int ranX = random.nextInt(800-1); // random value from 0 to width
int ranY = random.nextInt(800-1); // random value from 0 to height
window.getChildren().add(ranY, dot);
}
}, 0, 5000);
// create listener
dot.setOnMouseClicked(new EventHandler<MouseEvent>()
{
public void handle(MouseEvent e){
if(e.getSource()==dot)
{
score = score + 10;
if(score == 50)
{
popUp(primaryStage);
}
}
}
});
}
public void popUp(final Stage primaryStage)
{
primaryStage.setTitle("You won!");
final Popup popup = new Popup();
popup.setX(300);
popup.setY(200);
Text t = new Text("You won! Nice job!");
Text tt = new Text("Play again?");
Button yes = new Button("yes");
Button no = new Button("no");
popup.getContent().addAll(t, tt, yes, no);
yes.setOnAction(e -> Yes());
no.setOnAction(e -> No());
}
public void Yes()
{
restartGame();
}
public void No()
{
System.exit(0);
}
public void restartGame()
{
score = 0;
}
public static void main(String[] args)
{
launch(args);
}
}
错误日志:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at CatchTheDot.start(CatchTheDot.java:34)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application CatchTheDot
答案 0 :(得分:2)
Window
和Dot
已声明但未定义
答案 1 :(得分:1)
在第34行,跟踪说:
window.getChildren().add(dot);
那时window
或dot
(或两者)都未初始化。因此空指针异常。