-6

I am trying to make a basic game in which a dot is moved randomly around the screen, and when it is clicked, the user gets some points. When I run the program, I get a NullPointerException at the start method. I am wondering what I am doing wrong as well as any types for improving the program. Thank you!

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);
}

}

Error log:

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
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Adam7397
  • 57
  • 1
  • 7

2 Answers2

2

Window and Dot have been declared but not defined

Rana
  • 1,675
  • 3
  • 25
  • 51
1

In line 34, as the trace says:

window.getChildren().add(dot);

either window or dot (or both) are not initialized by then. Hence the null pointer exception.

Michał Szydłowski
  • 3,261
  • 5
  • 33
  • 55