gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:09:37 30 4
gpt4 key购买 nike

我目前正在尝试编写一个简单的 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)情况:

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)

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

最佳答案

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

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

关于javafx 程序中的 java.lang.reflect.InitationTargetException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52517105/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com