英文:
NullPointerException after building jar
问题
我有一个使用IntelliJ开发的JavaFX项目,在那里运行程序一切正常。但是当我运行mvn clean package
并尝试运行构建的jar文件时,出现以下“异常”:
at de.application.component.main.FilterListCell.setParent(FilterListCell.java:212)
at de.application.controller.MainController.lambda$initialize$2(MainController.java:183)
at javafx.scene.control.skin.ListViewSkin.createCell(ListViewSkin.java:432)
...
导致失败的行是:
this.parent = parent;
root.prefWidthProperty().bind(parent.widthProperty().subtract(16)); // this fails
}```
`root`为空,我不明白为什么,因为我在控制器中声明了它:
@FXML
public AnchorPane root;
这是fxml文件:
```
我不知道为什么会出现这种情况,我检查了IntelliJ使用与我相同的JDK版本。也许这与我的pom.xml
有关?
这是pom.xml
内容:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>de.group</groupId>
<artifactId>application</artifactId>
<version>0.1-SNAPSHOT</version>
<properties>
<java.version>11</java.version>
<javafx.version>11.0.2</javafx.version>
<maven.compiler.plugin.version>3.8.1</maven.compiler.plugin.version>
</properties>
<dependencies>
<!-- JavaFX dependencies -->
...
<!-- Spring Boot dependencies -->
...
<!-- Other dependencies -->
...
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
所有帮助将不胜感激。
编辑
MainController
中的代码片段是这样的:
lstFilter.setCellFactory(compositeFilterListView -> {
FxControllerAndView<FilterListCell, Node> cell = fxWeaver.load(FilterListCell.class);
if(cell.getView().isPresent()) {
cell.getView().get().setVisible(false);
cell.getController().setGraphic(cell.getView().get());
}
cell.getController().setParent(lstFilter); // setParent fails
return cell.getController();
});
这只是lstFilter
列表视图的CellFactory
。真正的问题是在IntelliJ中运行时不会出现此错误,而在通过命令行运行构建的jar文件时会失败。
英文:
I have a JavaFX project that i develop with IntelliJ and when i run the program from there everythin works fine.<br/><br/>
But when i run mvn clean package
and try to run the jar that is built i get the following Exception
:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at de.application.component.main.FilterListCell.setParent(FilterListCell.java:212)
at de.application.controller.MainController.lambda$initialize$2(MainController.java:183)
at javafx.scene.control.skin.ListViewSkin.createCell(ListViewSkin.java:432)
at javafx.scene.control.skin.ListViewSkin.lambda$new$9(ListViewSkin.java:207)
at javafx.scene.control.skin.VirtualFlow.getCell(VirtualFlow.java:1672)
at javafx.scene.control.skin.VirtualFlow.getCellLength(VirtualFlow.java:1801)
at javafx.scene.control.skin.VirtualFlow.computeViewportOffset(VirtualFlow.java:2639)
at javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1245)
at javafx.scene.Parent.layout(Parent.java:1204)
at javafx.scene.Parent.layout(Parent.java:1211)
at javafx.scene.Parent.layout(Parent.java:1211)
at javafx.scene.Parent.layout(Parent.java:1211)
at javafx.scene.Parent.layout(Parent.java:1211)
at javafx.scene.Parent.layout(Parent.java:1211)
at javafx.scene.Parent.layout(Parent.java:1211)
at javafx.scene.Parent.layout(Parent.java:1211)
at javafx.scene.Parent.layout(Parent.java:1211)
at javafx.scene.Parent.layout(Parent.java:1211)
at javafx.scene.Parent.layout(Parent.java:1211)
at javafx.scene.Parent.layout(Parent.java:1211)
at javafx.scene.Scene.doLayoutPass(Scene.java:576)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2482)
at com.sun.javafx.tk.Toolkit.lambda$runPulse$2(Toolkit.java:412)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:411)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:438)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:519)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:499)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:492)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$11(QuantumToolkit.java:320)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)
This is the line that fails:
public void setParent(ListView<CompositeFilter> parent) {
this.parent = parent;
root.prefWidthProperty().bind(parent.widthProperty().subtract(16)); // this fails
}
root
is being null what I don't understand because I declare it in the controler like this
@FXML
public AnchorPane root;
and this is the fxml file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane fx:id="root" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity" prefHeight="200.0" xmlns="http://javafx.com/javafx/11.0.1"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="de.application.component.main.FilterListCell">
<HBox alignment="CENTER_LEFT" AnchorPane.leftAnchor="8.0" AnchorPane.topAnchor="8.0" AnchorPane.rightAnchor="8.0">
<!-- some content -->
</HBox>
</AnchorPane>
I have no idea why this is the case, i checked that IntelliJ uses the same JDK as i do.<br/><br>
Maybe this is an issue with my pom?
Here it is:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>de.group</groupId>
<artifactId>application</artifactId>
<version>0.1-SNAPSHOT</version>
<properties>
<java.version>11</java.version>
<javafx.version>11.0.2</javafx.version>
<maven.compiler.plugin.version>3.8.1</maven.compiler.plugin.version>
</properties>
<dependencies>
<!-- JavaFX -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-base</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.controlsfx</groupId>
<artifactId>controlsfx</artifactId>
<version>11.0.1</version>
</dependency>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<!-- Other -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>net.rgielen</groupId>
<artifactId>javafx-weaver-spring</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
All help will be greatly appreciated.
EDIT
The code snippet from the MainController is this
lstFilter.setCellFactory(compositeFilterListView -> {
FxControllerAndView<FilterListCell, Node> cell = fxWeaver.load(FilterListCell.class);
if(cell.getView().isPresent()) {
cell.getView().get().setVisible(false);
cell.getController().setGraphic(cell.getView().get());
}
cell.getController().setParent(lstFilter); // setParent fails
return cell.getController();
});
This is just a CellFactory for the Listview lstFilter
.
The real problem is the discrepency between running through Intellij, where this error does not occur and running the built jar trough the command line, where this fails.
答案1
得分: 1
所以我找到了解决办法:
问题是我的 fxml 文件找不到。我仍然不确定为什么。
控制器类被称为 FilterListView
,而我的 fxml 文件是 filterListView.fxml
。
现在我将 fxml 文件重命名为 FilterListView.fxml
,然后它就正常工作了。
有谁知道为什么在运行 JAR 文件时类路径似乎是大小写敏感的,而在从集成开发环境运行时不区分大小写?
英文:
So I figured out a solution to my problem:
The prolem was that my fxml file was not found. I am still not sure why.
The controller class was called FilterListView
and my fxml file filterListView.fxml
.
Now i renamed the fxml file to FilterListView.fxml
and it works.
Does anyone know why the classpath seems to be case-sensitive when running a jar and not case-sensitive when running from the IDE?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论