在 java xml 应用程序中初始化控制器时的空指针
Posted
技术标签:
【中文标题】在 java xml 应用程序中初始化控制器时的空指针【英文标题】:Nullpointer when initializing controller in javafxml application 【发布时间】:2019-06-20 12:19:15 【问题描述】:我没有使用 fxml 的经验,并且总体上缺乏编码知识。所以我尝试制作一个带有多个标签和文本字段的 fxml 应用程序。我希望控制器在初始化中为所有文本字段添加侦听器,以便在视图中编辑文本时更新模型。我无法让控制器加载。
我尝试初始化控制器,并且尝试让 fxml 加载器初始化控制器。两者都在 TextField 属性上给出了空指针异常。此外,当我调用 loader.getController 它返回 null。 第一次放代码,如果格式不好请见谅
应用类
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class Main extends Application
@Override
public void start(Stage primaryStage) throws Exception
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
Controller c = loader.getController();
System.out.println(c==null);
public static void main(String[] args)
launch(args);
部分 FXML 类:
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/8.0.172-ea"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<HBox prefHeight="30.0" prefWidth="600.0">
<children>
<Label prefHeight="30.0" prefWidth="200.0" text="Name" />
<TextField fx:id="name" prefHeight="31.0" prefWidth="198.0" />
<Button fx:id="button" mnemonicParsing="false" prefHeight="31.0" prefWidth="130.0" text="Button">
<HBox.margin>
<Insets left="10.0" />
</HBox.margin>
</Button>
</children>
</HBox>
(...)More Hboxes(...)
控制器类
public class Controller
Model model;
@FXML private Button button;
@FXML private TextField name;
@FXML private TextField kcal;
public Controller()
initialize();
@FXML
public void initialize()
model = new Model();
name.textProperty().addListener(((observable, oldValue, newValue)
->
model.setName(newValue);
));
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
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(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
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$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException:
"path"/FoodFirebaseWriter/sample/sample.fxml:10
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at sample.Main.start(Main.java:16)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(WinApplication.java:191)
... 1 more
Caused by: java.lang.NullPointerException
at sample.Controller.initialize(Controller.java:38)
at sample.Controller.<init>(Controller.java:33)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)
at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
... 17 more
Exception running application sample.Main
【问题讨论】:
【参考方案1】:我知道我做错了什么。首先调用构造函数,然后初始化@fxml 属性,然后调用initialize 方法。我在构造函数中调用了初始化方法。然后@fxml 属性为null,并调用了两次initialize 方法。所以我只是删除了构造函数来解决问题。
【讨论】:
以上是关于在 java xml 应用程序中初始化控制器时的空指针的主要内容,如果未能解决你的问题,请参考以下文章
使用 jackson-dataformat-xml 库将 XML 中的空标签转换为 java 属性
邮递员插件将值[FromBody]传递给发布方法时的空值[重复]