0

When I try to run the application by the jar file via the console, during the login event, it gives an exception "java.lang.NullPointerException: Location is required." But when I run the application using the intellij IDE it works fine.

@FXML
public void loginAction(ActionEvent event) throws Exception {
    errorLabel.setText("");
    authenticate(event);
}

    private void authenticate(Event event) throws Exception {

    if (validateInput()) {
        String username = userName.getText().trim();
        User user = UserDAO.searchUserByName(username);

        if (user == null) {
            userName.setText("");
            password.setText("");
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("Login Error");
            alert.setHeaderText("Failure message");
            alert.setContentText("Invalid username!!");
            alert.showAndWait();

        } else {

            String userPassword = user.getPassword();
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(password.getText().getBytes());
            byte byteData[] = md.digest();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < byteData.length; i++) {
                sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));

            }

            String role = user.getRole();
            String id = user.getUserId();
            sessionRole = role;
            userId = id;

            if (sb.toString().equals(userPassword)) {

                if (role.equals("Admin")) {
                    windows("../views/admin.fxml", "Admin Dashboard");
                } else {
                    windows("../views/pos.fxml", "Point of Sales");
                }
            } else {
                userName.setText("");
                password.setText("");
                Alert alert = new Alert(Alert.AlertType.ERROR);
                alert.setTitle("Login Error");
                alert.setHeaderText("Failure message");
                alert.setContentText("Invalid password!!");
                alert.showAndWait();

            }

        }

    }
}
   private void windows(String path, String title) throws Exception {
    Stage stage;
    Parent root;
    root = FXMLLoader.load(getClass().getResource(path));
    stage = (Stage) loginButton.getScene().getWindow();
    Scene scene = new Scene(root);
    stage.setTitle(title);
    stage.setScene(scene);
    stage.show();
}

it gives the exception to the line:

windows("../views/admin.fxml", "Admin Dashboard");

I tried to give the absolute path also for the fxml file. It only occurs when I run the application using the jar file. No file is missing in the jar also. What can be the cause of this. Thanks in advance.

Ragnar921
  • 829
  • 3
  • 17
  • 30
  • I'm quite ignorant about how relative paths work. But i had the same issue a week ago, and what i found to be working for me is writing my relative paths in this format `//path/to/file.fxml`. – DarkMental Jan 17 '18 at 06:50
  • If it is a part of your project (or a resource) you should probably use `getResource`. See, e.g., https://stackoverflow.com/questions/6400646/how-should-i-use-getresource-in-java – Itai Jan 17 '18 at 09:22

1 Answers1

1

.. works on resources on the filesystem, as it an actual entry on the filesystem, that points to parent directory. But that does not exist within jars, so it won't work to point to the parent directory when the classes & resources are packaged in a jar.

So we cant use .. to indicate the file locations, hence we have to use the absolute path within the project path.

Ragnar921
  • 829
  • 3
  • 17
  • 30
  • You don't *have* to use absolute paths: you can get these relative to a different class (which I prefer). So typically I break my project into different packages in which the controller and corresponding FXML are in the same package (e.g. I have a package `mainview` with `MainView.fxml` and `MainController.class`, and another package `editor` with `Editor.fxml` and `EditorController.class`). Then you can just load the FXML relative to its controller class, e.g. `load(EditorController.class.getResource("Editor.fxml"))`. – James_D Jan 17 '18 at 14:36
  • Yes that also seems sensible. Thanks – Ragnar921 Jan 18 '18 at 03:28