2

I'm trying to play audio file in my JavaFX app, code look fine for me but I'm getting errors

I think that the problem is in line 28:

"Caused by: java.lang.NullPointerException: Cannot invoke "java.net.URL.toString()" because "resource" is null at sample.Main.start(Main.java:28)"

why resource is null? in my file structure I got resource package, but it doesn't matter where audio file is, error is the same

enter image description here

I'm new in coding, so I don't know how to exactly read these errors... if anyone could help, would be great

code and errors belove

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;

import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.IOException;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws UnsupportedAudioFileException, IOException, LineUnavailableException
    {
        String css = this.getClass().getResource("styles.css").toExternalForm();
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        root.getStylesheets().add(css);
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();

        final java.net.URL resource = getClass().getResource("test2.mp3");
        final Media media = new Media(resource.toString());
        final MediaPlayer mediaPlayer = new MediaPlayer(media);
        mediaPlayer.play();
    }

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

after run this code I;m getting these errors

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
    at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NullPointerException: Cannot invoke "java.net.URL.toString()" because "resource" is null
    at sample.Main.start(Main.java:28)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    ... 1 more
Exception running application sample.Main

Process finished with exit code 1

thx for any help!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 4
    Note you don't need Java Sound imports when using JavaFX's media API. Also note you should maintain a strong reference to the `MediaPlayer`; if it's garbage collected the media will stop playing. As for the error, `getResource(String)` returns null if the resource could not be found using the specified path. Check out [How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?](https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other) for some help with that. – Slaw Jan 12 '22 at 22:36
  • thx for advices, i gonna do it right now! – Patryk Kobyłka Jan 12 '22 at 22:37
  • 1
    As far as I can tell, `"test2.mp3"` should be `"/sample/test2.mp3"`. – Andrew Thompson Jan 13 '22 at 02:57
  • @AndrewThompson, I'm thinking since the class Main is in the same folder as the audio file, the relative form may actually be correct, even if your form, with the audio file in the resources folder would be better. The method getClass() will be referring to the Main class and it's folder location, yes? This stuff can get confusing. – Phil Freihofner Jan 14 '22 at 09:55
  • @Slaw, I'm curious why you didn't put this comment into an Answer area. It seems to me to be a valid answer, not a request for clarifying info as the Stackoverflow guidelines suggest for this area. – Phil Freihofner Jan 14 '22 at 09:57
  • @PhilFreihofner *"This stuff can get confusing."* That's a large part of the reason I would never, ever, *ever* use anything but the explicit 'path from the root of the class path'. Why guess or be confused? – Andrew Thompson Jan 14 '22 at 11:36
  • @PhilFreihofner Because the question is, in my opinion, a duplicate of the one I linked in my comment, but I didn't feel like using my gold badge to single-handedly make that decision. The other notes are auxiliary to the problem. – Slaw Jan 14 '22 at 18:03
  • @Slaw Thanks for the explanation. As I did not see the phrase "duplicate question" I didn't make that connection. You can indicate this and put it up for a vote without using your gold badge, yes? – Phil Freihofner Jan 14 '22 at 21:31
  • 1
    @PhilFreihofner No, I don't think I can avoid the gold badge (but maybe I'm wrong). In any case, the answer posted by the OP seems to agree that their question is a duplicate and so I've marked it as such. – Slaw Jan 14 '22 at 21:57

1 Answers1

2

As @Slaw wrote problem was with path to resource, there is more information in link belove,

THX Slaw!

"Note you don't need Java Sound imports when using JavaFX's media API. Also note you should maintain a strong reference to the MediaPlayer; if it's garbage collected the media will stop playing. As for the error, getResource(String) returns null if the resource could not be found using the specified path. Check out How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application? for some help with that."