start方法中的JavaFX InvocationTargetException

时间:2015-02-21 19:19:59

标签: java javafx singleton

所以,我试图创建一个我写的javafx应用程序,但由于某种原因,很多东西都在破坏。我试图使用Singleton设计模式,但实际上我并不认为这是问题所在。当我尝试获取Display类的实例时代码中断。如果有人可以提供帮助,我会非常感激!我已经尝试了四个小时来调试这个...我通过添加一个公共构造函数删除了Singleton模式但是没有帮助。谢谢!

这是我的主要课程:

public class Main extends Application {

@Override
public void start (Stage s) {
    View view = new View(s);
    view.init();
}

public static void main (String[] args) {
    launch(args);
}
}

这是在Main中创建的View类:

public class View {
private Stage myStage;
private Model myModel;
private static final ResourceBundle myValues = ResourceBundle.getBundle(
        "view.resources/display/values", new Locale("view"));
private Display myDisplay;
private String myResourcesLocation = "resources.languages/English";

public View(Stage s) {
    myStage = s;
    myModel = new Model();
}

public void init() {
    myStage.setTitle(myValues.getString("Title"));
    myDisplay =  Display.getInstance(myStage, (Receiver) myModel);
    Scene scene = myDisplay.getScene();
    myStage.setScene(scene);
    myStage.show();
}

}

以下是从视图调用的显示:

public class Display {
private static Display instance;
private Stage myStage;
private Scene myScene;
private BorderPane myRoot;
private MenuBar myMenuBar;
private Workspace myWorkspace;
private static Feed myFeed;
private static final ResourceBundle myValues = ResourceBundle.getBundle(
        "resources/display/values", new Locale("display"));

public Display(Stage stage, Receiver myReceiver) {
    myStage = stage;
    myRoot = new BorderPane();
    myRoot.setBottom(myFeed.getInstance(myReceiver));
    myRoot.setTop(makeMenuBar());
    // setupMenuBar();
    myRoot.setCenter(makeWorkspace());
    myScene = new Scene(myRoot, Integer.parseInt(myValues
            .getString("Width")), Integer.parseInt(myValues
            .getString("Height")));
    myStage.setScene(myScene);
    myStage.show();
}

protected static Display getInstance(Stage stage, Receiver myReceiver) {
    if (instance == null)
        instance = new Display(stage, myReceiver);
    return instance;
}
private Node makeMenuBar() {

    MenuBar menuBar = new MenuBar();
    try {
        menuBar.getMenus().add(makeMenu("File"));
        menuBar.getMenus().add(makeMenu("Edit"));
        menuBar.getMenus().add(makeMenu("View"));
        menuBar.getMenus().add(makeMenu("Options"));
        menuBar.getMenus().add(makeMenu("Help"));
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return menuBar;
}

private Menu makeMenu(String name) throws NoSuchMethodException,
        IllegalAccessException, IllegalArgumentException,
        InvocationTargetException {
    Menu menu = new Menu(name);
    String[] arrayCharles = myValues.getString(name).split(", ");
    for (String s : arrayCharles) {
        MenuItem item = new MenuItem();
        Method m = Display.class.getDeclaredMethod(getMethodName(s));
        // not sure why I'm getting this error
        // item.setOnAction(e -> m.invoke(null, null));
        menu.getItems().add(item);
    }
    return menu;
}

private String getMethodName(String s) {
    s.replaceAll(" ", "");
    String first = String.valueOf(s.charAt(0));
    s.replaceFirst("%c", first.toLowerCase());
    return s;
}

public Scene getScene() {
    return this.myScene;
}


private Workspace makeWorkspace() {
    return new Workspace();
}
}

最后,以下是我收到的错误消息:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:363)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:303)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
    at com.sun.javafx.application.LauncherImpl$$Lambda$49/1732398722.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ExceptionInInitializerError
    at view.View.init(View.java:37)
    at Main.start(Main.java:13)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
    at com.sun.javafx.application.LauncherImpl$$Lambda$52/1459627066.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
    at com.sun.javafx.application.PlatformImpl$$Lambda$46/1051754451.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/480204181.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/1775282465.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Caused by: java.util.MissingResourceException: Can't find bundle for base name resources/display/values, locale display
    at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1564)
    at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1387)
    at java.util.ResourceBundle.getBundle(ResourceBundle.java:845)
    at view.Display.<clinit>(Display.java:26)
    ... 12 more
Exception running application Main

1 个答案:

答案 0 :(得分:1)

问题是当你的程序试图加载资源时。

private static final ResourceBundle myValues = ResourceBundle.getBundle(
        "resources/display/values", new Locale("display"));

堆栈跟踪说明了所有内容:

Caused by: java.util.MissingResourceException: Can't find bundle for base name resources/display/values, locale display