构建jar后出现NullPointerException

时间:2020-05-04 11:04:03

标签: java maven javafx java-11

我有一个用IntelliJ开发的JavaFX项目,当我从那里运行程序时,一切都可以正常工作。

但是当我运行mvn clean package并尝试运行构建的jar时,我得到以下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)

这是失败的行:

public void setParent(ListView<CompositeFilter> parent) {
    this.parent = parent;
    root.prefWidthProperty().bind(parent.widthProperty().subtract(16)); // this fails
}

root为空,我不了解,因为我在这样的控件中声明了

    @FXML
    public AnchorPane root;

这是fxml文件:

<?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>

我不知道为什么会这样,我检查了IntelliJ是否使用与我相同的JDK。

也许这是我pom的问题?

这里是:

<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>

所有帮助将不胜感激。

编辑

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();
        });

这只是Listview lstFilter的CellFactory。 真正的问题是通过Intellij运行(不会发生此错误)与通过命令行运行内置的jar(失败)之间的差异。

1 个答案:

答案 0 :(得分:1)

所以我想出了解决问题的方法:

问题是找不到我的fxml文件。我仍然不确定为什么。 控制器类名为FilterListView,而我的fxml文件名为filterListView.fxml。 现在,我将fxml文件重命名为FilterListView.fxml,它可以正常工作。

有人知道为什么在运行jar时类路径似乎区分大小写,而从IDE运行时为什么不区分大小写吗?