我正在为局域网应用程序创建带有日志的表。当我尝试运行此应用程序时,它将引发NullPointerException,但fx:id和fx:controller是正确的。我正在使用JavaFX 12。
我试图用Intellij重构fx:id和fx:controller,并且Intellij也在logPane.fxml和LogPaneController.class中正确更改了此设置,但是仍然无法正常工作。如果我在引发异常的注释行中,所有应用程序都可以正常工作。我用谷歌搜索“ FXML组件返回NUllPointerException”,但结果始终是“您的fx:id或fx:controller名称在控制器中不相同”。
logPane.fxml
db.json
LogPaneController.class
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane HBox.hgrow="ALWAYS" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.lanssmaker.controller.LogPaneController">
<children>
<TableView fx:id="logTable" prefHeight="574.0" prefWidth="382.0" AnchorPane.bottomAnchor="0.0"
AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn maxWidth="140.0" minWidth="70" prefWidth="70.0" text="Time"/>
<TableColumn text="Content"/>
<TableColumn maxWidth="140.0" minWidth="70" prefWidth="70.0" text="Category"/>
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY"/>
</columnResizePolicy>
</TableView>
</children>
</AnchorPane>
异常日志
public class LogPaneController {
@FXML
private TableView<Log> logTable; //it's null
public TableView<Log> getLogTableView() {
return logTable;
}
public void initialize() {
//example ussage calling NullPointerException
logTable.isHover();
}
}
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:567)
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:567)
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:835)
Caused by: javafx.fxml.LoadException:
/D:/Programming/Java/javastart/ssmaker/target/classes/fxml/buttonsPane.fxml
/D:/Programming/Java/javastart/ssmaker/target/classes/fxml/mainPane.fxml:17
at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2595)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader$IncludeElement.constructValue(FXMLLoader.java:1154)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:754)
at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
at ssmaker/com.lanssmaker.main.Main.start(Main.java:16)
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:389)
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)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
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:567)
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:567)
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:2591)
... 22 more
Caused by: java.lang.NullPointerException
at ssmaker/com.lanssmaker.controller.LogPaneController.initialize(LogPaneController.java:25)
... 33 more
mainPane.fxml
package com.lanssmaker.main;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Pane mainPane = FXMLLoader.load(getClass().getResource("/fxml/mainPane.fxml"));
Scene scene = new Scene(mainPane);
stage.setScene(scene);
stage.setTitle("LAN ScreenShots Maker");
stage.show();
}
}
答案 0 :(得分:3)
仔细研究一下堆栈跟踪:
...
Caused by: javafx.fxml.LoadException:
/D:/Programming/Java/javastart/ssmaker/target/classes/fxml/buttonsPane.fxml
/D:/Programming/Java/javastart/ssmaker/target/classes/fxml/mainPane.fxml:17
...
Caused by: java.lang.NullPointerException
at ssmaker/com.lanssmaker.controller.LogPaneController.initialize(LogPaneController.java:25)
这意味着当buttonsPane.fxml
处理
FXMLLoader
时会发生异常
<fx:include source="buttonsPane.fxml"/>
元素。
此外,它告诉我,您不仅将LogPaneController
用于logPane.fxml
,而且还将buttonsPane.fxml
用于<fx:include>
。每次加载fxml时,都会创建单独的控制器实例。使用FXMLLoader
会导致创建一个单独的logTable
实例来加载包含的fxml。 null
字段仅为其中之一注入;对于其他控制器实例,它仍为buttonsPane.fxml
。 (这甚至忽略了这样的事实,即在加载logPane.fxml
之前先进行加载buttonsPane.fxml
,因此即使在完成加载TableView
时使用了相同的控制器实例,logPane.fxml
也不会是根据buttonsPane.fxml
创建的。)
为logPane.fxml
和isHover
使用单独的控制器类。仅使用同一控制器类就不会使两个fxml都“通信”。我建议您使用另一种方法,但是您在这里所做的操作没有任何意义(除非您尝试引发NPE):hover
只是重新调整了false
属性的值Scene
用于尚未属于et_pass = findViewById(id.et_pass);
Person p = new Person(et_fn.getText().toString(),et_sn.getText().toString(),et_em.getText().toString(),et_pass.getText().toString());
dbref.child(dbref.push().getKey()).setValue(p);
的节点。
答案 1 :(得分:0)
您有私有logTable的getter类,因此可以获取一个值,但是我看不到值初始化或setter类(它将初始化值)。