1

I am currently trying to code a simple MediaPlayer to explore javafx. (beginner here) I want to open a mp3 file and simply play it but when i launch it there is an exception ive never seen.

Heres the code:

@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();
}

And the 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)

Already tried to find a solution but cant really find anything. Thanks for the help.

ekelix
  • 11
  • 1
  • 2
  • is the path you use in new Media(...) correct? – Ralf Renz Sep 26 '18 at 11:56
  • `System.getProperty("Benutzer.dir")` returns `null`. – fabian Sep 26 '18 at 12:48
  • @fabian This question can't be considered a duplicate for the provided link, the problem is that he's incorrectly accessing the resources for his application, also he's not asking what's a null pointer exception he's asking how to solve the problem of accessing resources. – Bara' Hashesh Sep 26 '18 at 13:05

1 Answers1

0

If you read the stack of error you will notice that the error is caused by a null pointer exception at line 13 of the code can you kindly highlight that line, not sure which one.

Also when getting resources it's best to use the getClass().getResourcesAsStream() or getClass().getResource() methods.