0

I'm trying to write a simple music play in Java. I'm attempting to use toggle button to make the music play or pause, depending on whether the player is playing currently. Here's what my codes looks like right now:

public class Client extends Application {
private boolean isPlaying = false;

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

public void start(Stage primaryStage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("clientUI.fxml"));

    ToggleButton toggleButton = (ToggleButton) root.lookup("toggleButton");
    File song = new File("Shed.mp3");
    Media media = new Media(song.toURI().toURL().toExternalForm());
    MediaPlayer mp = new MediaPlayer(media);

    toggleButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
            if (isPlaying()) {
                mp.pause();
            } else {
                mp.play();
            }
        }
    });

    Scene s = new Scene(root);
    primaryStage.setScene(s);
    primaryStage.setResizable(false);
    primaryStage.setTitle("Noisey");
    primaryStage.show();

}

public boolean isPlaying() {
    return isPlaying;
}

} Upon running, however, this error is recieved: `

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(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
    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(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at application.Client.start(Client.java:32)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
    ... 1 more
Exception running application application.Client

Something tells me this lineCaused by: java.lang.NullPointerException at application.Client.start(Client.java:32) is the problem.

Charles
  • 51
  • 7
  • I suspect `root.lookup("toggleButton")` is returning null. You probably meant `"ToggleButton"` or `".toggle-button"` or `"#toggleButton"`. – VGR Feb 26 '16 at 00:31
  • Lookups won't work until CSS has been applied, which doesn't happen by default until the component actually appears on the screen. Why are you using a lookup at all, instead of the standard mechanism of using a controller class? – James_D Feb 26 '16 at 02:28
  • Sorry for the duplicate post, got confused there. I'm very new to javaFX, could you explain the standard mechanism of using a controller class? – Charles Feb 26 '16 at 02:30
  • Providing a tutorial is somewhat beyond the scope of a forum like this. Have a look at maybe [this tutorial](http://code.makery.ch/library/javafx-8-tutorial/part1/), which seems very popular, or the [Oracle tutorial](http://docs.oracle.com/javase/8/javafx/fxml-tutorial/fxml_tutorial_intermediate.htm). Or just read the [controllers section](http://docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/introduction_to_fxml.html#controllers) of the FXML documentation. – James_D Feb 26 '16 at 02:37

0 Answers0