Search code examples
javafxfxmlscenescenebuilder

NullPointerException in changing javaFX scene


I have two scenes in my javaFx project .. the first one Language.fxml has a button which on click changes the scene to allDevices.fxml .. but it throws NullPointerException saying "Location is required" although both of the fxml files are in the same path !!

that's my LanguageController.java

package astrolabe;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

/**
 *
 * @author Ahmed Fawzy
 */
public class LanguageController implements Initializable {

    @FXML
    private Button arabic ;


    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        arabic.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                // TODO Auto-generated method stub

                try{
                    Node node=(Node) event.getSource();
                    Stage stage=(Stage) node.getScene().getWindow();
                    Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("allDevices.fxml"));/* Exception */
                    Scene scene = new Scene(root);
                    stage.setScene(scene);
                    stage.show();
                }
                catch(Exception ex)
                {
                    ex.printStackTrace();
                }

            }
        });


    }

Solution

  • The problem solved by adding the package name before the fxml file name !