I am trying to play mp3 file and getting an exception. Here is my code and stack trace. My sound is in my workspace.
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);
}
}
This is just simple code for playing mp3 however i get an exception, Here is stack trace
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
The problem is here :
URL resource = URL resource = getClass().getResource("/Alistirma/src/IkinciAlistirma/hated.mp3");
First : Your mp3 file is not reachable from your workspace with getResource(String path)
method. Depending on your environment and IDE, it will sarch for "Alistirma/src/IkinciAlistirma/hated.mp3" in different place in your project. You can find the start point folder where the getResource(String path)
is searching for your resource by printing the URL given by getResource(".")
and then deduct where you want to put your file. If you want to keep your file where it is, you still can reach it with the File
class.
Second : why URL resource = URL resource =
? copy/paste fail I suppose ;) ?