我试图从另一个控制器类引用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
答案 0 :(得分:2)
所以我认为你有
Main.fxml
<!-- root element -->< ... fx:controller="MainController">
...
<fx:include source="foo.fxml"/>
...
如果是这样,请在fx:include:
上添加一个fx:id<fx:include fx:id="foo" source="foo.fxml"/>
然后你可以使用@FXML注释将foo.fxml的控制器注入MainController:
public class MainController {
@FXML
private FooController fooController ;
// ...
}
这里的规则是,如果include的fx:id是“x”,那么注入的控制器的变量名必须是“xController”。请参阅FXML简介中的Nested Controller。