0

Here i have that kind problem -

I need to change scene. Stage and root scene i have in main file:

package sample;

public class Main extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(Objects.requireNonNull(
                getClass().getResource("scenses/sample.fxml")));
        stage.setTitle("BlogApp");
        stage.setScene(new Scene(root, 600, 400));
        stage.show();
    }

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

Same i have controller, with login button. Login happen only IF(user found), and scene must be changed to main one:

button_auth.setOnAction(event -> {
    login_auth.setStyle("-fx-border-color: #fafafa");
    pass_auth.setStyle("-fx-border-color: #fafafa");
    button_reg.setText("Sign Up");

    if (login_auth.getCharacters().length() < 3) {
        login_reg.setStyle("-fx-border-color: red");
        return;
    } else if (pass_auth.getCharacters().length() < 3) {
        pass_auth.setStyle("-fx-border-color: red");
        return;
    }

    String pass = md5String(pass_auth.getCharacters().toString());
    try {
        boolean isAuth = db.authUser(login_auth.getCharacters().toString(), pass);
        if (isAuth) {
            login_auth.setText("");
            pass_auth.setText("");
            button_auth.setText("Logged in");
            Parent root = FXMLLoader.load(Objects.requireNonNull(
                    getClass().getResource("scenses/secondScene.fxml")));
            Scene homepage_scene = new Scene(root);
            Stage window = (Stage) ((Node) event.getSource()).getScene().getWindow();
            window.setScene(homepage_scene);
            window.show();
        } else {
            button_auth.setText("Login is denied");
        }
    } catch (SQLException | ClassNotFoundException | IOException e) {
        e.printStackTrace();
    }
});

That gives me error:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at java.base/java.util.Objects.requireNonNull(Objects.java:221)
at sample.controllers.regController.lambda$initialize$1(regController.java:114)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8889)
at javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208)
at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3856)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1851)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2584)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:412)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)

Error line 114 is

 Parent root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("scenses/secondScene.fxml")));

I am new in Java, so i actually dont understand why that happen. Thx for help anyway.

  • it fails at the Object.requireNonNull which means it probably cant find a resource -> you sure there is no typo in resource name? "scenses" sounds wrong.. – J Asgarov Apr 17 '21 at 14:27
  • You get a NullPointerException in a lambda expression in the class regController in line 114. What exactly is there? – mayamar Apr 17 '21 at 14:27
  • line 114 there is "Parent root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("scenses/secondScene.fxml")));" – Roman Pappinen Apr 17 '21 at 14:34

2 Answers2

0

Actually, I don't quite understand what you want to do. But you gave your getResource path wrong. That's why you can't get what you want to achieve and you get a NullPointerExcepiton . Can you update it as follows? Can you write as a comment if it is working?

Parent root = FXMLLoader.load(Objects.requireNonNull(
                getClass().getResource("sample.fxml")));
Yusuf BESTAS
  • 91
  • 1
  • 6
-2

I found it. Problem was with path. In main file i have path like that:

Parent root = FXMLLoader.load(Objects.requireNonNull(
            getClass().getResource("scenses/sample.fxml")));

I tried make same in controller file. But it was wrong.

So controller works now with that path:

Parent root = FXMLLoader.load(Objects.requireNonNull(
                getClass().getResource("sample/scenses/secondScene.fxml")));

Path mast be to the root.

Solved. Thx to all for help.

  • that's wrong - periods in the path are not supported (even though it might work in an IDE, it will fail in other contexts, f.i. in a jar) Make sure you read the duplicate to work out the correct path to your resource. – kleopatra Apr 17 '21 at 15:53
  • I know, but still i found solution, i will try to fix that to end only tomorrow :). Problem was in path... – Roman Pappinen Apr 17 '21 at 21:41
  • Just use a full path instead of the relative one, then. This is what I do anyways. Your "solution" is just – as @kleopatra pointed out – to error prone. – dzim Apr 19 '21 at 06:48