Magesh
Magesh

Reputation: 233

Java FX nullpointer stage.show() java 8

I am getting the below exception, when i export the code as runnable jar and run it via command prompt. The application is not getting loaded

If I directly run this main method in ecllipse IDE, I am not able to replicate this error

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)

Below given is the application start implementation

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

Getting nullpointer from primaryStage.show();

Below given is the entire application class

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

Upvotes: 0

Views: 1337

Answers (2)

CLOVIS
CLOVIS

Reputation: 998

I just had the exact same stacktrace, and I found the problem in my case. I don't think this is the problem you had, but since this is the first google result for that stacktrace, I think it's appropriate to give this solution too.

In my case the problem was a call to Font.loadFont(String) on a non-existing file. For some reason the NullPointerException is NOT thrown there, however, but is instead thrown when JavaFX internally tries to computer the text height.

Upvotes: 2

Magesh
Magesh

Reputation: 233

The Issue is becuase of Java version. There is bug in java version "1.8.0_25" related to com.sun.javafx.scene.control.skin.Utils.computeTextHeight().

The reason why it was working in my IDE is it uses JDK 1.8.

When I changed the version to 1.8 it works perfectly.

Upvotes: 0

Related Questions