I've recently started out with JavaFX and I am trying to make a Coin Toss application that shows a picture of the coin respective to what was rolled. I've already done a dice roll simulator with similar code to this but for some reason, I am receiving an error that I didn't for that.
CoinToss class
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
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;
public class CoinToss extends Application {
Image heads = new Image("D:\\Downloads - DATA\\heads1.png");
Image tails = new Image("D:\\Downloads - DATA\\tails1.png");
ImageView viewCoin;
Label resultCoin;
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) {
Label label1 = new Label("Welcome to the Coin Flipper!");
Label label2 = new Label("Please hit start to flip the coin!");
resultCoin = new Label();
Button startButton = new Button("Start!");
startButton.setOnAction(new StartButtonHandler());
VBox vbox = new VBox(10, label1,label2, startButton, viewCoin, resultCoin);
vbox.setPadding(new Insets(12));
vbox.setAlignment(Pos.TOP_CENTER);
Scene simulatorScene = new Scene(vbox,400,400);
primaryStage.setScene(simulatorScene);
primaryStage.show();
}
class StartButtonHandler implements EventHandler<ActionEvent>{
public void handle(ActionEvent event) {
int num = 0;
String word = "";
CoinValue value = new CoinValue();
value.roll1();
num = value.getRoll1();
if(num == 0) {
viewCoin = new ImageView(heads);
word = "heads";
} else {
viewCoin = new ImageView(tails);
word = "tails";
}
resultCoin.setText("You rolled a " + word);
}
}
}
CoinValue class
package application;
import java.util.Random;
public class CoinValue {
int rollnum;
public void roll1()
{
Random rand = new Random();
rollnum = rand.nextInt(1) + 1;
}
public int getRoll1()
{
return rollnum;
}
}
I have tried to find an answer from StackOverflow many times but I can't seem to find one that fits my issue since I am not using FXMLLoader.
Error Message
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
at java.base/java.lang.reflect.Method.invoke(Method.java:577)
at javafx.graphics@18.0.2/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
at javafx.graphics@18.0.2/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:577)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1081)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics@18.0.2/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
at javafx.graphics@18.0.2/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NullPointerException: Children: child node is null: parent = VBox@4ac2de6c
at javafx.graphics@18.0.2/javafx.scene.Parent$3.onProposedChange(Parent.java:542)
at javafx.base@18.0.2/com.sun.javafx.collections.VetoableListDecorator.addAll(VetoableListDecorator.java:233)
at javafx.base@18.0.2/com.sun.javafx.collections.VetoableListDecorator.addAll(VetoableListDecorator.java:103)
at javafx.graphics@18.0.2/javafx.scene.layout.VBox.<init>(VBox.java:252)
at HelloFX/application.CoinToss.start(CoinToss.java:34)
at javafx.graphics@18.0.2/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
at javafx.graphics@18.0.2/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at javafx.graphics@18.0.2/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics@18.0.2/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at javafx.graphics@18.0.2/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics@18.0.2/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics@18.0.2/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
... 1 more
Exception running application application.CoinToss