Java FX nullpointer stage.show()java 8

时间:2014-12-09 17:23:10

标签: javafx

当我将代码导出为runnable 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()获取nullpointer;

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

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

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

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