javafx程序中的java.lang.reflect.InvocationTargetException

时间:2018-09-26 11:51:49

标签: java javafx

我目前正在尝试编写一个简单的MediaPlayer来探索javafx。 (这里是初学者) 我想打开一个mp3文件并简单地播放它,但是当我启动它时,有一个我从未见过的例外。

此处提供代码:

@Override
public void start(Stage primaryStage) {
    Media pick = new Media("file:///" + System.getProperty("Benutzer.dir").replace('\\', '/') + "/" + "oof.mp3");
    MediaPlayer player = new MediaPlayer(pick);

    MediaView mediaView = new MediaView(player);

    Group root = new Group(mediaView);
    Scene scene = new Scene(root, 500, 200);

    primaryStage.setTitle("MediaPlayerTest");
    primaryStage.setScene(scene);
    primaryStage.show();

    player.play();
}

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

还有一个例外:

Exception in Application start method
java.lang.reflect.InvocationTargetException
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
 at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
 at soundEdit.start(soundEdit.java:13)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)

已经尝试寻找解决方案,但实际上找不到任何东西。 感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

如果您阅读错误堆栈,您会注意到该错误是由代码第13行中的空指针异常引起的,您能否请高亮显示该行,而不确定是哪一行。

另外,在获取资源时,最好使用getClass().getResourcesAsStream()getClass().getResource()方法。