0

我正在播放mp3文件並獲取異常。這是我的代碼和堆棧跟蹤。我的聲音在我的工作區中。JavaFX正在播放AudioClip,InvocationTargetException

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只是簡單的代碼,但是我得到一個異常,這裏是堆棧跟蹤

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 
+0

可能重複[什麼是空指針異常,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how -do-i-fix-it) – hotzst

回答

0

的問題是在這裏:

URL resource = URL resource = getClass().getResource("/Alistirma/src/IkinciAlistirma/hated.mp3"); 

第一:你的MP3文件無法通過getResource(String path)方法從工作區訪問。根據您的環境和IDE,它將在項目中的不同位置搜索「Alistirma/src/IkinciAlistirma/hated.mp3」。您可以通過打印getResource(".")給出的URL找到getResource(String path)搜索資源的起始點文件夾,然後扣除要放置文件的位置。如果你想保留你的文件,你仍然可以通過File課程獲得。

二:爲什麼URL resource = URL resource =?複製/粘貼失敗,我想;)?

+0

當我創建這個問題時,我改變了一些東西,並從我的代碼中複製,我沒有看到那部分感謝。 ^^ – MertG

+0

好的,謝謝我剛剛修復它,感謝Kwoinkwoin。尋找getResource(「。」)工作。我的目標是src文件夾,但默認目標是bin文件夾。 – MertG