0

is there a way to change the scene by setOnEndOfMedia in javafx ? .. i tried this but it's throwing a NullPointerException

void change(Event event){
        try{
            Node node=(Node) event.getSource();
            Stage   stage=(Stage) node.getScene().getWindow();
            Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("astrolabe/astrolabe_intro.fxml"));/* Exception */

            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.setFullScreenExitHint("");

            stage.show();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }


    }

and calling change() on end of media like this

astrolabe_intro.setOnEndOfMedia(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                System.out.println("tewst");
                Event e = new Event(null) ;
                e.fireEvent(astrolabe_intro1, e);
                change( e);


            }
        });
Fawzinov
  • 569
  • 2
  • 9
  • 25
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – DavidPostill Oct 09 '14 at 08:07
  • Questions seeking debugging help ("**why isn't this code working?**") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it **in the question itself**. Questions without **a clear problem statement** are not useful to other readers. See [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – DavidPostill Oct 09 '14 at 08:08
  • On which line are you getting a `NullPointerException`? And can you give enough of your code, or at least an explanation, so we understand why you are trying to fire an event in the `onEndOfMedia` handler? – James_D Oct 09 '14 at 11:45

1 Answers1

1

If the exception is coming from the FXMLLoader.load(...) line, the chances are that the path to the fxml file is wrong. Log the value of the URL you get from the call to getResource(...) to check.

But also:

Why do you need the event at all? The only place you use it in the change method is to get a node and get hold of the window. You presumably have other references to some nodes (I'm assuming this is a controller.) Why not just do

void change(){
        try{
            Node node= ... ;// any node in your scene will do....
            Stage   stage=(Stage) node.getScene().getWindow();
            Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("astrolabe/astrolabe_intro.fxml"));/* Exception */

            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.setFullScreenExitHint("");

            stage.show();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }


    }

and then

astrolabe_intro.setOnEndOfMedia(new Runnable() {
    @Override
    public void run() {
        change();
    }
});

which in Java 8 you can abbreviate to

astrolabe_intro.setOnEndOfMedia(this::change);
James_D
  • 201,275
  • 16
  • 291
  • 322