首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >JavaFX Playing,InvocationTargetException

JavaFX Playing,InvocationTargetException
EN

Stack Overflow用户
提问于 2016-01-22 09:12:25
回答 1查看 596关注 0票数 0

我正在尝试播放mp3文件并得到一个异常。这是我的代码和堆栈跟踪。我的声音在我的工作空间里。

代码语言:javascript
运行
复制
public class Main extends Application {
//my code
    @Override 
    public void start(Stage primaryStage) {
        URL resource = getClass().getResource("/Alistirma/src/IkinciAlistirma/hated.mp3"); //its in my workspace
        AudioClip clip = new AudioClip(resource.toString()); //line 21

        final Button button = new Button("Play"); //simple button just for playing clip
        button.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                // TODO Auto-generated method stub
                clip.play();
            }
        });
               //after here nothing important for my problem.
               //basic FX stuff
    }

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

这只是播放mp3的简单代码,但是我得到了一个例外,下面是堆栈跟踪

代码语言:javascript
运行
复制
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:497)
    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(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    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:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
    at IkinciAlistirma.Main.start(Main.java:21)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$49(GtkApplication.java:139)
    ... 1 more
Exception running application IkinciAlistirma.Main
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-22 10:19:47

问题在于:

代码语言:javascript
运行
复制
URL resource = URL resource = getClass().getResource("/Alistirma/src/IkinciAlistirma/hated.mp3"); 

首先:使用mp3方法无法从工作区访问您的getResource(String path)文件。根据您的环境和IDE,它将在项目中不同的位置为"Alistirma/src/IkinciAlistirma/hated.mp3“提供sarch。通过打印getResource(String path)给出的URL,您可以找到getResource(".")正在搜索资源的起始点文件夹,然后扣除要放置文件的位置。如果您想将文件保存在它所在的位置,仍然可以使用File类访问它。

第二:为什么是URL resource = URL resource =?我想复制/粘贴失败了;)?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34942887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档