0

I've been trying to create a Dice Simulator, where I will create a JavaFX application that simulates rolling a pair of dice. When the user clicks a button, my application will generate two random numbers, each in the range of 1 through 6, to represent the value of the dice. My application will then display the dice by using ImageView controls.

When I try to run the code that I currently have, I get this error:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    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(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
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$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Children: child node is null: parent = HBox@60b7c6fe
    at javafx.scene.Parent$2.onProposedChange(Parent.java:435)
    at com.sun.javafx.collections.VetoableListDecorator.addAll(VetoableListDecorator.java:234)
    at com.sun.javafx.collections.VetoableListDecorator.addAll(VetoableListDecorator.java:103)
    at javafx.scene.layout.HBox.<init>(HBox.java:261)
    at DiceSimulator.start(DiceSimulator.java:42)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    ... 1 more
Exception running application DiceSimulator

I have no idea what went wrong since everything looks fine to me. Any help is appreciated thank you!

DieImages Class

import javafx.scene.image.Image;

public class DieImages 
{
    Image pic1 = new Image("file:images/1Die.bmp");
    Image pic2 = new Image("file:images/2Die.bmp");
    Image pic3 = new Image("file:images/3Die.bmp");
    Image pic4 = new Image("file:images/4Die.bmp");
    Image pic5 = new Image("file:images/5Die.bmp");
    Image pic6 = new Image("file:images/6Die.bmp");

    private int value;
    private Image dieImage = pic1;

    public void setImage(int sides)
    {
        int value = sides;
        if(value == 1)
            dieImage = pic1;
        if(value == 2)
            dieImage = pic2;
        if(value == 3)
            dieImage = pic3;
        if(value == 4)
            dieImage = pic4;
        if(value == 5)
            dieImage = pic5;
        if(value == 6)
            dieImage = pic6;
    }

    public Image getImage()
    {
        return dieImage;
    }

}

DieRoll Class

import java.util.Random;

public class DieRoll 
{
    int rollNum;

    public void roll()
    {
        Random rand = new Random(6);
        rollNum = rand.nextInt();
    }

    public int getRoll()
    {
        return rollNum;
    }
}

Dice Simulator Class

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import java.util.Random;

public class DiceSimulator extends Application
{
     private ImageView viewDie1;
     private ImageView viewDie2;
     private Label resultDie;
     private Label message1;
     private Label message2;

     public static void main(String[] args)
       {
          // Launch the application.
          launch(args);
       }

       @Override
       public void start(Stage primaryStage)
       {
           primaryStage.setTitle("Dice Simulator");
           Label message1 = new Label("Welcome to the Dice Simulator!");
           Label message2 = new Label("Please hit start to roll the dice!");

           resultDie = new Label();
           Button startButton = new Button("Start!");

           startButton.setOnAction(new StartButtonHandler()); 

           HBox hbox = new HBox(10, viewDie1, viewDie2);

           VBox vbox = new VBox(10, message1, message2, hbox, resultDie);
           vbox.setPadding(new Insets(15));
           vbox.setAlignment(Pos.CENTER);

           Scene simulatorScene = new Scene(vbox);
           primaryStage.setScene(simulatorScene);
           primaryStage.show();


       }

       class StartButtonHandler implements EventHandler<ActionEvent>
       {
           @Override
           public void handle(ActionEvent event)
           {
               int num1 = 0;
               int num2 = 0;
               Image diePic1 ; 
               Image diePic2;

               DieRoll dieI = new DieRoll();
               DieRoll dieII = new DieRoll();
               dieI.roll();
               dieII.roll();
               num1 = dieI.getRoll();
               num1 = dieI.getRoll();

               DieImages image1 = new DieImages();
               DieImages image2 = new DieImages();
               image1.setImage(num1);
               image1.setImage(num2);
               diePic1 = image1.getImage();
               diePic2 = image2.getImage();

               viewDie1 = new ImageView(diePic1);
               viewDie2 = new ImageView(diePic2);

               resultDie.setText("You rolled a " + num1 + " and " + num2 + "!");
           }
       }     
}

Ruberis
  • 3
  • 3
  • 1
    The information about the type of exception you're getting is pretty useless in this case. Every exception happening in the `start` method is wrapped in an `InvocationTargetException`. The actually valuable info about the source of the issue is contained in the causes; proably the last one is the most relevant one. Please do provide the full stacktrace in cases like this one. Even though you may currently not be able to pull the info required to identify the issue from there, it'll speed up things for us immensely, since we don't need to play the guessing game or create this probject ourselves – fabian Nov 17 '19 at 01:55
  • “I have no idea what went wrong since everything looks fine to me.” The purpose of the exception’s full stack trace is to give you an idea of what went wrong. Please edit your question and include it, including all `Caused by:` sections. – VGR Nov 17 '19 at 03:14
  • Thank you for letting me know that, I've added the entire error code I'm getting. – Ruberis Nov 17 '19 at 03:14
  • 1
    You don't appear to be initializing `viewDie1` or `viewDie2` before adding them to the `HBox`... – Slaw Nov 17 '19 at 06:02

0 Answers0