gpt4 book ai didi

java - 从另一个类引用 @FXML TextField,但收到 NullPointerException

转载 作者:行者123 更新时间:2023-11-30 04:05:22 24 4
gpt4 key购买 nike

我试图从另一个 Controller 类引用 TextField (@FXML),但出现 NullPointerException。以下代码解释了该项目。我想了解为什么会发生这种情况。感谢您提前做出的努力!

Foo.fxml 文件

//Foo.fxml
...
<TextField fx:id="textField" prefWidth="54" text="someText" />
...

FooController.java 文件

//FooController.java
public class FooController implements Initializable {

@FXML
private TextField textField;

public TextField gettextField(){
return textField;
}
}

MainController.java 文件

//MainController.java
public class MainController implements Initializable {

private FooController fooControllerClass;

@Override
public void initialize(URL url, ResourceBundle rb) {
System.out.println(fooControllerClass.gettextField().getText()); // this line throws java.lang.NullPointerException
}
}

Main.java 文件

//Main.java
public class Main extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {launch(args);}
}

堆栈跟踪:

java.lang.NullPointerException
file:/home/vaib/shared/NetBeansProjects/JRAD/dist/run1408613327/Main.jar!/jrad/Main.fxml
at jrad.MainController.initialize(MainController.java:6)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2185) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2185)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2061)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2778)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2757)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2743)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2730)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2719)
at jrad.Main.start(Main.java:34)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2185)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:216)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:89)
at java.lang.Thread.run(Thread.java:744)

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.javafx.main.Main.launchApp(Main.java:698)
at com.javafx.main.Main.main(Main.java:871)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NullPointerException
at jrad.JRADController.initialize(JRADController.java:196)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2185)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2061)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2778)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2757)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2743)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2730)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2719)
at jrad.JRAD.start(JRAD.java:34)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:216)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:89)
... 1 more
Java Result: 1

最佳答案

所以我认为你已经

Main.fxml

<!-- root element -->< ... fx:controller="MainController">
...
<fx:include source="foo.fxml"/>
...

如果是这样,请将 fx:id 放在 fx:include 上:

<fx:include fx:id="foo" source="foo.fxml"/>

然后您可以使用 @FXML 注释将 foo.fxml 的 Controller 注入(inject)到 MainController 中:

public class MainController {
@FXML
private FooController fooController ;
// ...
}

这里的规则是,如果包含的 fx:id 是“x”,那么注入(inject) Controller 的变量名称必须是“xController”。请参阅Nested Controller位于 FXML 简介中。

关于java - 从另一个类引用 @FXML TextField,但收到 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20908617/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com