【问题标题】:Java FX nullpointer stage.show() java 8Javafx空指针stage.show()java 8
【发布时间】:2014-12-09 17:23:09
【问题描述】:

当我将代码导出为可运行 jar 并通过命令提示符运行它时,出现以下异常。应用程序未加载

如果我直接在 ecllipse IDE 中运行这个 main 方法,我无法复制这个错误

java.lang.NullPointerException
    at com.sun.javafx.scene.control.skin.Utils.computeTextHeight(Unknown Source)
    at com.sun.javafx.scene.control.skin.LabeledSkinBase.computeMinLabeledPartHeight(Unknown Source)
    at com.sun.javafx.scene.control.skin.LabeledSkinBase.computeMinHeight(Unknown Source)
    at javafx.scene.control.Control.computeMinHeight(Unknown Source)
    at javafx.scene.Parent.minHeight(Unknown Source)
    at javafx.scene.layout.Region.minHeight(Unknown Source)
    at javafx.scene.layout.Region.computeChildMinAreaHeight(Unknown Source)
    at javafx.scene.layout.GridPane.computeMinHeights(Unknown Source)
    at javafx.scene.layout.GridPane.computeMinHeight(Unknown Source)
    at javafx.scene.Parent.minHeight(Unknown Source)
    at javafx.scene.layout.Region.minHeight(Unknown Source)
    at javafx.scene.layout.Region.computeChildPrefAreaHeight(Unknown Source)
    at javafx.scene.layout.AnchorPane.computeHeight(Unknown Source)
    at javafx.scene.layout.AnchorPane.computeMinHeight(Unknown Source)
    at javafx.scene.Parent.minHeight(Unknown Source)
    at javafx.scene.layout.Region.minHeight(Unknown Source)
    at javafx.scene.layout.Region.computeChildPrefAreaHeight(Unknown Source)
    at javafx.scene.layout.VBox.getAreaHeights(Unknown Source)
    at javafx.scene.layout.VBox.layoutChildren(Unknown Source)
    at javafx.scene.Parent.layout(Unknown Source)
    at javafx.scene.Parent.layout(Unknown Source)
    at javafx.scene.Parent.layout(Unknown Source)
    at javafx.scene.Scene.doLayoutPass(Unknown Source)
    at javafx.scene.Scene.preferredSize(Unknown Source)
    at javafx.scene.Scene.impl_preferredSize(Unknown Source)
    at javafx.stage.Window$9.invalidated(Unknown Source)
    at javafx.beans.property.BooleanPropertyBase.markInvalid(Unknown Source)
    at javafx.beans.property.BooleanPropertyBase.set(Unknown Source)
    at javafx.stage.Window.setShowing(Unknown Source)
    at javafx.stage.Window.show(Unknown Source)
    at javafx.stage.Stage.show(Unknown Source)
    at com.company.tool.ToolApp.start(ToolApp.java:28)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(Unknown Source)
    at com.sun.javafx.application.LauncherImpl$$Lambda$52/18309370.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$45/1115142.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$164(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/26202636.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/14208992.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$$Lambda$38/25518380.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

下面给出的是应用启动实现

    @Override
public void start(Stage primaryStage) throws Exception {
    try {
        System.out.println("Primary Stage: "+ primaryStage);
        System.out.println("Primary Stage: "+ primaryStage.getTitle());
        stage = primaryStage;
        stage.setTitle("Tool App");
        gotoStartPage();
        primaryStage.show();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

从primaryStage.show()获取空指针;

下面给出的是整个应用程序类

public class ToolApp extends Application {

private Stage stage;

@Override
public void start(Stage primaryStage) throws Exception {
    try {
        System.out.println("Primary Stage: "+ primaryStage);
        System.out.println("Primary Stage: "+ primaryStage.getTitle());
        stage = primaryStage;
        stage.setTitle("Tool App");
        gotoStartPage();
        primaryStage.show();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}


private void gotoStartPage() {
    try {
        replaceSceneContent("Tool.fxml");
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

private void replaceSceneContent(String fxml) throws Exception {
    FXMLLoader loader = new FXMLLoader();
    InputStream in = ToolApp.class.getResourceAsStream(fxml);
    loader.setBuilderFactory(new JavaFXBuilderFactory());
    loader.setLocation(ToolApp.class.getResource(fxml));
    AnchorPane page;
    try {
        page = (AnchorPane) loader.load(in);
    } finally {
        in.close();
    }
    ToolController toolController = loader.getController();
    try{
        toolController.initializeComponents();
    }catch(Exception e){
        e.printStackTrace();
    }

    Scene scene = new Scene(page, 800, 600);
    stage.setScene(scene);
    stage.sizeToScene();
}

/**
 * The main() method is ignored in correctly deployed JavaFX application.
 * main() serves only as fallback in case the application can not be
 * launched through deployment artifacts, e.g., in IDEs with limited FX
 * support. NetBeans ignores main().
 *
 * @param args
 *            the command line arguments
 */
public static void main(String[] args)
{
    launch(args);
}

【问题讨论】:

  • 查看您的堆栈跟踪,然后查看您发布的代码。提示:您至少需要向我们展示您在gotoStartPage()..中所做的事情。
  • 添加了代码详情
  • 对我来说这看起来不错。我会尝试删除stage.sizeToScene()。如果这没有帮助,我们需要查看 FXML 文件。
  • 这没有帮助。 gotoStartPage() 后的异常弹出窗口
  • 当我在其他机器上运行这个 jar 时它可以工作。任何屏幕布局问题?

标签: javafx


【解决方案1】:

我刚刚有完全相同的堆栈跟踪,我发现了我的问题。我认为这不是您遇到的问题,但由于这是该堆栈跟踪的第一个 google 结果,我认为也适合提供此解决方案。

在我的情况下,问题是在一个不存在的文件上调用Font.loadFont(String)。但是,由于某种原因,NullPointerException 没有被抛出,而是在 JavaFX 内部尝试计算文本高度时被抛出。

【讨论】:

  • 我在使用我的 jar 文件中打包的字体时遇到了类似的问题。如果我将其注释掉,应用程序就会运行。奇怪的是,该问题仅针对该系统上的一个用户出现。如果我登录并运行 jar 一切正常并显示字体。有什么想法吗?
  • 好吧,如果我遇到了同样的问题,有可能它在 JAR 中找不到字体文件,因为路径可能不同
【解决方案2】:

问题是因为 Java 版本。 com.sun.javafx.scene.control.skin.Utils.computeTextHeight() 相关的 java 版本“1.8.0_25”中存在错误。

它在我的 IDE 中工作的原因是它使用 JDK 1.8。

当我将版本更改为 1.8 时,它可以完美运行。

【讨论】:

  • 1.8.0_131 版仍然存在同样的问题...有什么建议吗?
  • 1.8.0_171 中同样的问题
猜你喜欢
  • 1970-01-01
  • 2014-05-18
  • 2016-07-11
  • 2016-08-01
  • 2017-03-21
  • 2015-06-12
相关资源
最近更新 更多