空指针异常在构建 JAR 后发生。

huangapple go评论118阅读模式
英文:

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 &quot;JavaFX Application Thread&quot; 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&lt;CompositeFilter&gt; 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:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;?import javafx.geometry.*?&gt;
&lt;?import javafx.scene.control.*?&gt;
&lt;?import javafx.scene.layout.*?&gt;
&lt;AnchorPane fx:id=&quot;root&quot; maxHeight=&quot;-Infinity&quot; maxWidth=&quot;-Infinity&quot; minHeight=&quot;-Infinity&quot;
            minWidth=&quot;-Infinity&quot; prefHeight=&quot;200.0&quot; xmlns=&quot;http://javafx.com/javafx/11.0.1&quot;
            xmlns:fx=&quot;http://javafx.com/fxml/1&quot;
            fx:controller=&quot;de.application.component.main.FilterListCell&quot;&gt;
  &lt;HBox alignment=&quot;CENTER_LEFT&quot; AnchorPane.leftAnchor=&quot;8.0&quot; AnchorPane.topAnchor=&quot;8.0&quot; AnchorPane.rightAnchor=&quot;8.0&quot;&gt;
   &lt;!-- some content --&gt;
  &lt;/HBox&gt;
&lt;/AnchorPane&gt;

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:

&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  &lt;parent&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
    &lt;version&gt;2.1.7.RELEASE&lt;/version&gt;
    &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
  &lt;/parent&gt;
  &lt;groupId&gt;de.group&lt;/groupId&gt;
  &lt;artifactId&gt;application&lt;/artifactId&gt;
  &lt;version&gt;0.1-SNAPSHOT&lt;/version&gt;

  &lt;properties&gt;
    &lt;java.version&gt;11&lt;/java.version&gt;
    &lt;javafx.version&gt;11.0.2&lt;/javafx.version&gt;
    &lt;maven.compiler.plugin.version&gt;3.8.1&lt;/maven.compiler.plugin.version&gt;
  &lt;/properties&gt;

  &lt;dependencies&gt;
    &lt;!-- JavaFX --&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.openjfx&lt;/groupId&gt;
      &lt;artifactId&gt;javafx-controls&lt;/artifactId&gt;
      &lt;version&gt;${javafx.version}&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.openjfx&lt;/groupId&gt;
      &lt;artifactId&gt;javafx-fxml&lt;/artifactId&gt;
      &lt;version&gt;${javafx.version}&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.openjfx&lt;/groupId&gt;
      &lt;artifactId&gt;javafx-graphics&lt;/artifactId&gt;
      &lt;version&gt;${javafx.version}&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.openjfx&lt;/groupId&gt;
      &lt;artifactId&gt;javafx-base&lt;/artifactId&gt;
      &lt;version&gt;${javafx.version}&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;org.controlsfx&lt;/groupId&gt;
      &lt;artifactId&gt;controlsfx&lt;/artifactId&gt;
      &lt;version&gt;11.0.1&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;!-- Spring Boot --&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
    &lt;/dependency&gt;

    &lt;!-- Other --&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
      &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;com.fasterxml.jackson.module&lt;/groupId&gt;
      &lt;artifactId&gt;jackson-module-parameter-names&lt;/artifactId&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;com.fasterxml.jackson.datatype&lt;/groupId&gt;
      &lt;artifactId&gt;jackson-datatype-jdk8&lt;/artifactId&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;com.fasterxml.jackson.datatype&lt;/groupId&gt;
      &lt;artifactId&gt;jackson-datatype-jsr310&lt;/artifactId&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;net.rgielen&lt;/groupId&gt;
      &lt;artifactId&gt;javafx-weaver-spring&lt;/artifactId&gt;
      &lt;version&gt;1.2.0&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-configuration-processor&lt;/artifactId&gt;
      &lt;optional&gt;true&lt;/optional&gt;
    &lt;/dependency&gt;

    &lt;!-- Test --&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
      &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;
  &lt;/dependencies&gt;

  &lt;build&gt;
    &lt;plugins&gt;
      &lt;plugin&gt;
        &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
        &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
        &lt;version&gt;${maven.compiler.plugin.version}&lt;/version&gt;
        &lt;configuration&gt;
          &lt;source&gt;${java.version}&lt;/source&gt;
          &lt;target&gt;${java.version}&lt;/target&gt;
        &lt;/configuration&gt;
      &lt;/plugin&gt;
      &lt;plugin&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
      &lt;/plugin&gt;
    &lt;/plugins&gt;
  &lt;/build&gt;

&lt;/project&gt;

All help will be greatly appreciated.

EDIT

The code snippet from the MainController is this

lstFilter.setCellFactory(compositeFilterListView -&gt; {
            FxControllerAndView&lt;FilterListCell, Node&gt; 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?

huangapple
  • 本文由 发表于 2020年5月4日 19:04:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/61590731.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定