Reputation: 23
I am trying to get two different choice box to work at the same time However I am getting a nullpointer error in the initialize method. which is kind of weird cuz I am trying to initialize what is in the choice box but the IDE is giving me a nullpointer exception.
this is my code
package application;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.Parent;
import javafx.stage.Stage;
public class SampleController implements Initializable{
public static piece PieceClicked = new piece();
ArrayList <Integer> possibleMoves = new ArrayList <Integer>();
ListView<Integer> listView = new ListView<Integer>();
private int enemyLocation;
private boolean valid = true;
@FXML
ChoiceBox<String> inputPieceType, inputPieceAllience;
String[] pieces = {"CANNON","CHARIOT","ELAPHANT","GENERAL","GUARD","HORSES","SOLDIERS"};
String[] allience = {"GREEN","RED"};
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
inputPieceType.getItems().addAll(pieces);
inputPieceAllience.getItems().addAll(allience);
inputPieceType.setOnAction(this::setPiece);
inputPieceAllience.setOnAction(this::setAllience);
}
public void setPiece(ActionEvent event) {
PieceClicked.setPieceType(inputPieceType.getValue());
}
public void setAllience(ActionEvent event) {
PieceClicked.setPieceAllience(inputPieceAllience.getValue());
}
}
it is also telling me that I have an error in my main class at
Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));
my main class is this
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));
Scene scene = new Scene(root, 600, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
and the error message was this
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: javafx.fxml.LoadException:
/Users/owenpark/eclipse/java-2020-06/Eclipse.app/Contents/MacOS/computerIA/computerScienceIA/bin/application/Sample.fxml
at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2707)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2677)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3323)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3280)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3249)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3222)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3199)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3192)
at application.Main.start(Main.java:13)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2673)
... 14 more
Caused by: java.lang.NullPointerException
at application.SampleController.initialize(SampleController.java:62)
... 25 more
Exception running application application.Main
what could I do to help with this?
edit: my fxmlfile this is the scene with all the choice boxes.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>
<AnchorPane prefHeight="403.0" prefWidth="839.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SampleController">
<children>
<AnchorPane layoutX="1.0" layoutY="2.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="837.0">
<children>
<ImageView fitHeight="310.0" fitWidth="312.0" layoutX="14.0" layoutY="45.0">
<image>
<Image url="@../../../../../../../../../Documents/computer%20science%20IA%20photos/thumb-data_illust_Jang_Gi_Pan_1440x1440.jpeg" />
</image>
</ImageView>
<VBox layoutX="443.0" layoutY="43.0" prefHeight="315.0" prefWidth="146.0">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="location: " />
<TextField fx:id="input" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="piece: " />
<ChoiceBox fx:id="inputPieceType" prefWidth="150.0" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Allience: " />
<ChoiceBox fx:id="inputPieceAllience" prefWidth="150.0" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="enemy piece location: " />
<TextField fx:id="inputEnemyPieceLocation" prefHeight="32.0" prefWidth="146.0" />
</children>
</VBox>
<Button layoutX="332.0" layoutY="52.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="33.0">
<graphic>
<ImageView fitHeight="22.0" fitWidth="27.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../../../../../../../../Documents/computer%20science%20IA%20photos/Green_King.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button layoutX="332.0" layoutY="91.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="33.0">
<graphic>
<ImageView fitHeight="22.0" fitWidth="26.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../../../../../../../../Documents/computer%20science%20IA%20photos/Green_Cha.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button layoutX="332.0" layoutY="133.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="33.0">
<graphic>
<ImageView fitHeight="21.0" fitWidth="25.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../../../../../../../../Documents/computer%20science%20IA%20photos/Green_Cannon.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button layoutX="332.0" layoutY="171.0" mnemonicParsing="false" prefHeight="32.0" prefWidth="42.0">
<graphic>
<ImageView fitHeight="23.0" fitWidth="24.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../../../../../../../../Documents/computer%20science%20IA%20photos/green_Elephant.jpeg" />
</image>
</ImageView>
</graphic>
</Button>
<Button layoutX="332.0" layoutY="214.0" mnemonicParsing="false" prefHeight="31.0" prefWidth="42.0">
<graphic>
<ImageView fitHeight="20.0" fitWidth="37.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../../../../../../../../Documents/computer%20science%20IA%20photos/Green_Horse.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button layoutX="332.0" layoutY="255.0" mnemonicParsing="false" prefHeight="33.0" prefWidth="42.0">
<graphic>
<ImageView fitHeight="20.0" fitWidth="67.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../../../../../../../../Documents/computer%20science%20IA%20photos/Green_Soldier.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button layoutX="332.0" layoutY="296.0" mnemonicParsing="false" prefHeight="33.0" prefWidth="42.0">
<graphic>
<ImageView fitHeight="20.0" fitWidth="33.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../../../../../../../../Documents/computer%20science%20IA%20photos/Green_Guard.png" />
</image>
</ImageView>
</graphic>
</Button>
<Text layoutX="39.0" layoutY="56.0" strokeType="OUTSIDE" strokeWidth="0.0" text="1 2 3 4 5 6 7 8 9" wrappingWidth="270.7308011274751" />
<Text layoutX="29.0" layoutY="72.0" strokeType="OUTSIDE" strokeWidth="0.0" text="1" wrappingWidth="8.67726926690625" />
<Text layoutX="29.0" layoutY="98.0" strokeType="OUTSIDE" strokeWidth="0.0" text="2" wrappingWidth="8.67726926690625" />
<Text layoutX="29.0" layoutY="129.0" strokeType="OUTSIDE" strokeWidth="0.0" text="3" wrappingWidth="8.67726926690625" />
<Text layoutX="27.0" layoutY="157.0" strokeType="OUTSIDE" strokeWidth="0.0" text="4" wrappingWidth="8.67726926690625" />
<Text layoutX="27.0" layoutY="189.0" strokeType="OUTSIDE" strokeWidth="0.0" text="5" wrappingWidth="8.67726926690625" />
<Text layoutX="29.0" layoutY="218.0" strokeType="OUTSIDE" strokeWidth="0.0" text="6" wrappingWidth="8.67726926690625" />
<Text layoutX="29.0" layoutY="248.0" strokeType="OUTSIDE" strokeWidth="0.0" text="7" wrappingWidth="8.67726926690625" />
<Text layoutX="29.0" layoutY="279.0" strokeType="OUTSIDE" strokeWidth="0.0" text="8" wrappingWidth="8.67726926690625" />
<Text layoutX="29.0" layoutY="309.0" strokeType="OUTSIDE" strokeWidth="0.0" text="9" wrappingWidth="8.67726926690625" />
<Text layoutX="22.0" layoutY="335.0" strokeType="OUTSIDE" strokeWidth="0.0" text="10" wrappingWidth="20.65190019321369" />
<Button fx:id="enterButton" layoutX="452.0" layoutY="359.0" mnemonicParsing="false" onAction="#enterButton" text="Input" />
<Button fx:id="clear" layoutX="516.0" layoutY="359.0" mnemonicParsing="false" onAction="#clear" text="clear" />
<Text layoutX="380.0" layoutY="72.0" strokeType="OUTSIDE" strokeWidth="0.0" text="GENERAL" />
<Text layoutX="381.0" layoutY="110.0" strokeType="OUTSIDE" strokeWidth="0.0" text="CHARIOT" />
<Text layoutX="382.0" layoutY="153.0" strokeType="OUTSIDE" strokeWidth="0.0" text="CANNON" />
<Text layoutX="379.0" layoutY="189.0" strokeType="OUTSIDE" strokeWidth="0.0" text="ELEPHANT" />
<Text layoutX="382.0" layoutY="234.0" strokeType="OUTSIDE" strokeWidth="0.0" text="HORSE" />
<Text layoutX="382.0" layoutY="276.0" strokeType="OUTSIDE" strokeWidth="0.0" text="SOLDIER" />
<Text layoutX="386.0" layoutY="317.0" strokeType="OUTSIDE" strokeWidth="0.0" text="GUARD" />
<VBox layoutX="589.0" layoutY="42.0" prefHeight="317.0" prefWidth="126.0">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="location:" />
<Label fx:id="locationCheck" prefHeight="25.0" prefWidth="126.0" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="piece Type: " />
<Label fx:id="pieceCheck" prefHeight="36.0" prefWidth="126.0" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Allience: " />
<Label fx:id="allianceCheck" prefHeight="31.0" prefWidth="128.0" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="enemy piece location:" />
<Label fx:id="enemyLocationCheck" prefHeight="33.0" prefWidth="127.0" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="error message:" wrappingWidth="123.269287109375" />
<Label fx:id="errorMessage" prefHeight="35.0" prefWidth="129.0" />
</children>
</VBox>
<Text layoutX="607.0" layoutY="28.0" strokeType="OUTSIDE" strokeWidth="0.0" text="validity check" />
<Text layoutX="723.0" layoutY="27.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Possible Possition" />
<Text layoutX="710.0" layoutY="301.0" strokeType="OUTSIDE" strokeWidth="0.0" text="can this piece be taken" />
<Label fx:id="PieceTake" layoutX="708.0" layoutY="305.0" prefHeight="23.0" prefWidth="126.0" />
</children>
</AnchorPane>
<ListView fx:id="listView" layoutX="715.0" layoutY="40.0" prefHeight="248.0" prefWidth="113.0" />
<Button fx:id="exitButton" layoutX="779.0" layoutY="361.0" mnemonicParsing="false" onAction="#exitButton" prefHeight="27.0" prefWidth="47.0" text="exit" />
</children>
</AnchorPane>
sample.fxml file is a different scene and basically it is just a starting scene where the user can choose between two different scenes to go to
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SampleController">
<children>
<Text layoutX="143.0" layoutY="53.0" strokeType="OUTSIDE" strokeWidth="0.0" text="welcome to janggi!">
<font>
<Font size="40.0" />
</font>
</Text>
<Button fx:id="gameButton" layoutX="205.0" layoutY="85.0" mnemonicParsing="false" onAction="#gameButton" prefHeight="74.0" prefWidth="190.0" text="Game Play">
<font>
<Font size="22.0" />
</font>
</Button>
<Button fx:id="explanationButton" layoutX="205.0" layoutY="225.0" mnemonicParsing="false" onAction="#explanationButton" prefHeight="74.0" prefWidth="190.0" text="Explanation">
<font>
<Font size="22.0" />
</font>
</Button>
</children>
</AnchorPane>
Upvotes: 1
Views: 138
Reputation: 138
Edit:
The actual problem was that SampleController
was linked to two different .fxml
files and only the Sample.fxml
file was being loaded, which didn't contain the ChoiceBox
es. This can be fixed by having two separate controllers, one for each .fxml
file, and loading them both.
Old answer:
In JavaFX controllers, fields marked with the @FXML
tag have their values injected by the JavaFX-runtime from the controls defined in the .fxml
file associated with the controller.
In your case, you've got two ChoiceBox<String>
instances in the controller, so you'll need two ChoiceBox
es defined in your .fxml
-file as well. To allow the runtime to know which control to link to which field in the controller, you'll need to add an fx:id="fieldName"
on each of the controls in the .fxml
file. Don't forget to change the value from fieldName
to the corresponding name of the field in the controller, in your case inputPieceType
on one of them and inputPieceAllience
on the other.
The .fxml
file would then look something like this:
<ChoiceBox fx:id="inputPieceType" [other properties...] />
If you are unsure if the controller class is connected to your .fxml
file, one way you can see this is by looking for the fx:controller="com.example.MyController"
property inside of your .fxml
file. The com.example.MyController
will be the full name of the controller class, which in your case should be application.SampleController
.
Note that you can also set the controller in code instead of in .fxml
by calling fxmlLoader.setController(controller);
when loading the .fxml
file. Which one you use depends on your use case.
Upvotes: 4
Reputation: 41
Make sure your .fxml file which you are loading in main method is on correct path (classpath).
If you verified, then please make sure variable names you declared in controller with @FXML annotation @FXML ChoiceBox inputPieceType, inputPieceAllience; are same or no typo in .fxml file by comparing it. If it does not match then javafx will unable to autowire ChoiceBox class in Controller and resulting null pointer exception.
Just a suggestion whenever you make changes(UI Components) using Scene Builder please refresh your .fxml files since eclipse sometimes take time to refresh it.
Upvotes: 0
Reputation: 166
The issue is that you never say new ChoiceBox<String>
, so when you reference them with inputPieceType.getItems().addAll(pieces);
or inputPieceAllience.getItems().addAll(allience);
on line 62 they are calling methods for an item that was never created.
You could do something like this to initialize the objects:
...
private int enemyLocation;
private boolean valid = true;
@FXML
ChoiceBox<String> inputPieceType = new ChoiceBox<String>();
@FXML
ChoiceBox<String> inputPieceAllience = new ChoiceBox<String>();
String[] pieces = {"CANNON","CHARIOT","ELAPHANT","GENERAL","GUARD","HORSES","SOLDIERS"};
String[] allience = {"GREEN","RED"};
...
Upvotes: -3