【发布时间】: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