当前位置: 首页 > 知识库问答 >
问题:

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

松安民
2023-03-14

我试图让两个不同的选择框同时工作,但是我在initialize方法中得到一个nullpointer错误。这有点奇怪,因为我正在尝试初始化选择框中的内容,但IDE却给了我一个nullpointer异常。

这是我的密码

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

它还告诉我,在父根=fxmlLoader.load(getClass().getResource(“sample.fxml”));处的主类中有一个错误

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

共有1个答案

鄢修德
2023-03-14

编辑:实际问题是samplecontroller链接到两个不同的.fxml文件,并且只加载了sample.fxml文件,该文件不包含ChoiceBoxES。这个问题可以通过两个单独的控制器来解决,每个.fxml文件一个控制器,并加载这两个控制器。

老答案:在JavaFX控制器中,标记有@fxml标记的字段的值由JavaFX-runtime从与控制器关联的.fxml文件中定义的控件中注入。

在您的示例中,控制器中有两个ChoiceBox 实例,因此您还需要在.fxml文件中定义两个ChoiceBoxE。要允许运行库知道哪个控件要链接到控制器中的哪个字段,您需要在.fxml文件中的每个控件上添加fx:id=“fieldname”。不要忘记将fieldname的值更改为控制器中字段的相应名称,在您的示例中,其中一个字段的名称为InputPieceType,另一个字段的名称为InputPieceallience

.fxml文件如下所示:

<ChoiceBox fx:id="inputPieceType" [other properties...] />

如果您不确定controller类是否连接到.fxml文件,您可以通过查找.fxml文件中的fx:controller=“com.example.mycontroller”属性来了解这一点。com.example.myController将是controller类的全名,在您的示例中,该类应该是application.sampleController

请注意,在加载.fxml文件时,还可以通过调用FXMLLoader.SetController(controller);来在代码中而不是在.fxml中设置控制器。您使用哪一个取决于您的用例。

 类似资料:
  • 下面是用eclipse IDE编写的Java代码... 线程“main”java.lang.NullPointerException在compile.execute.main(execute.java:17)中出现异常 你能告诉我如何解决这个错误吗?

  • 我有一个RESTful API的Java项目,我使用Jackson/jersey来处理从支持AngularJS的web应用程序发送的传入JSON POST数据。 我们在1.8版(通过Maven)中使用jersey-json,因为这是我们公司推荐的版本。 接下来的课一切都很顺利 即使我们决定使用带有空构造函数和公共setter的私有成员,它仍然可以正常工作。 但是我们想让这个类是不可变的。经过一些研

  • 我使用 我编写了一些以块结尾的jruby代码,在运行所有代码后,出现了NullPointerException。但代码以任何其他语句结尾,不会发生异常。 版本:1.7.19 在ARGV。java vars.get变量返回null,因为在BiVariableM中isReceiverIdentical返回falseap.java 在ISReceiver中,此方法只需将接收器与双变量的接收器usgin'

  • 当使用阴影和最小化Jar时,我从阴影插件中得到一个NullpointerException。 在下面的示例中,我定义了两个依赖项;junit和Commons-lang3,在我的代码中,只使用了Commons-lang3-中的一个类。我对阴影留档的理解是未使用的类不会包含在阴影罐子中,所以我希望阴影罐子只包含我的类和StringUtils。 运行 时,我得到以下输出(mvn 干净包的完整输出 -X

  • 问题内容: 我正在上课的酒店管理软件上工作,我的代码遇到了一些问题。此时,我只是想将我在单独的类中创建的JPanel添加到主gui中。任何帮助将不胜感激。〜谢谢! 注意:错误发生在“ jpanel.add(“ Room”,room.getRoomPanel());“行上 代码:HotelSystem.java RoomSystem.java 问题答案: 您从未初始化 即使您确实对其进行了初始化,您

  • 问题内容: 我一直试图创建一个包含两个值的类的数组,但是当我尝试将值应用于该数组时,我得到了。 为什么会出现此异常,我该如何解决? 问题答案: 你创建了数组,但未在其中放置任何内容,因此你有一个包含5个元素的数组,所有元素均为null。你可以添加 在设置的行之前。