Java 使用choiceBox和数组时出现NullpointerException错误

Java 使用choiceBox和数组时出现NullpointerException错误,java,javafx,Java,Javafx,我试图让两个不同的选择框同时工作,但是在initialize方法中出现了null指针错误。这有点奇怪,因为我试图初始化选择框中的内容,但是IDE给了我一个空指针异常 这是我的密码 package application; import java.net.URL; import java.util.ArrayList; import java.util.Iterator; import java.util.ResourceBundle; import javafx.event.ActionEve

我试图让两个不同的选择框同时工作,但是在initialize方法中出现了null指针错误。这有点奇怪,因为我试图初始化选择框中的内容,但是IDE给了我一个空指针异常

这是我的密码

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());
    }
}
错误信息是这样的

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
我能做些什么来帮助你

编辑: 我的FXML文件 这是包含所有选择框的场景

<?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文件是一个不同的场景,基本上它只是一个开始场景,用户可以在两个不同的场景中进行选择

<?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>

问题在于,您从未说过
新选项框
,因此当您使用
inputPieceType.getItems().addAll(pieces)引用它们时
inputPieceAllience.getItems().addAll(allience)在第62行,他们正在调用从未创建过的项的方法

您可以执行以下操作来初始化对象:

。。。
私人互联网地址;
private boolean valid=true;
@FXML
ChoiceBox inputPieceType=新的ChoiceBox();
@FXML
ChoiceBox inputPieceAllience=新的ChoiceBox();
弦[]件={“大炮”、“战车”、“埃拉芬特”、“将军”、“卫兵”、“马”、“士兵”};
字符串[]allience={“绿色”,“红色”};
...
  • 确保在main方法中加载的.fxml文件位于正确的路径(类路径)

  • 如果您进行了验证,请确保您在控制器中声明的变量名带有@FXML注释 @FXML 选择框inputPieceType、inputPieceAllience; 比较.fxml文件是否存在相同的键入错误。若不匹配,javafx将无法在控制器中自动连接ChoiceBox类,并导致空指针异常

  • 只是一个建议,每当您使用场景生成器进行更改(UI组件)时,请刷新您的.fxml文件,因为eclipse有时需要时间来刷新它

  • 编辑: 实际问题是,
    SampleController
    链接到两个不同的
    .fxml
    文件,并且只加载了
    Sample.fxml
    文件,该文件不包含
    ChoiceBox
    es。这可以通过使用两个单独的控制器来解决,每个
    .fxml
    文件对应一个控制器,然后同时加载这两个控制器

    旧答案: 在JavaFX控制器中,标记有
    @FXML
    标记的字段的值由JavaFX运行时从与控制器关联的
    .FXML
    文件中定义的控件注入

    在您的例子中,控制器中有两个
    ChoiceBox
    实例,因此您还需要在
    .fxml
    -文件中定义两个
    ChoiceBox
    实例。要让运行时知道哪个控件要链接到控制器中的哪个字段,您需要在
    .fxml
    文件中的每个控件上添加一个
    fx:id=“fieldName”
    。不要忘记将值从
    fieldName
    更改为控制器中字段的相应名称,在您的情况下,其中一个字段的值为
    inputPieceType
    ,另一个字段的值为
    inputPieceAllience

    .fxml
    文件将如下所示:

    <ChoiceBox fx:id="inputPieceType" [other properties...] />
    
    
    
    如果不确定控制器类是否连接到
    .fxml
    文件,可以通过查找
    .fxml
    文件中的
    fx:controller=“com.example.MyController”
    属性来查看。
    com.example.MyController
    将是控制器类的全名,在您的例子中应该是
    application.SampleController

    请注意,您还可以在cod中设置控制器
    <ChoiceBox fx:id="inputPieceType" [other properties...] />