0

I am success to change the pane in stack pane with .setVisible() when the button is in the main scene, not in the stack pane.

But when I want to change my pane with clicking the button in one of the pane , I'll get the NullPointer error......

I tried to create the StackPane controller in every pane controller, and use the method .isPressed() to controll the pane visible , so how can I fix this problem?

chatController.java

public class chatController{
    @FXML Pane pane_chat_list,pane_chat_room;

    public void initialize() {
        pane_chat_list.setVisible(false);
        pane_chat_room.setVisible(true);
    }
    public void isPressed(int a) {
        if(a == 0) {
             pane_chat_list.setVisible(true);
             pane_chat_room.setVisible(false);
         }else {
             pane_chat_list.setVisible(false);
             pane_chat_room.setVisible(true);
         }
    }
}

chat_list.java

public class chat_list{
    @FXML Button chat_list_button;
    chatController controll = new chatController();
    public void initialize() {
        chat_list_button.setOnAction(e -> back());
    }
    public void back() {
        controll.isPressed(1);
    }
}

chat_room.java

public class chat_room{
    @FXML Button chat_room_back;
    chatController controll = new chatController();
    public void initialize() {
        chat_room_back.setOnAction(e -> back());
    }
    public void back() {
        controll.isPressed(0);
    }
}
Anita Chang
  • 13
  • 1
  • 4

1 Answers1

0

My first suggestion is to read some good Java book or tutorial, and there are many good resources for Java, also make sure to learn conventions.

Your problem is that you create new instance of chatController in both chat_room and chat_list, and they should share same instance in other to make it work, also you probably don't even initialize pane_chat_list and pane_chat_room inside chatController which leads to NullPointerException.

Your approach makes this really hard even though solution is much simpler, all you need is to have 1 parent class which holds both views, views don't know about each other, they just inform parent that there was a click on backButton, and parent manages what is shown and what is hidden.

FilipRistic
  • 2,261
  • 4
  • 20
  • 26
  • I know what you're saying, but I think I have initialize them in my chatController, I have edit my problem for the chatController.java , and I really don't know why it can't work... I think it look like the same with the solution you suggested... – Anita Chang Jul 26 '18 at 00:46
  • Problem is that @FXML annotation isn't magical, you can't write it anywhere and expect to get initialized field, it works only when using `FXMLLoader` which will inject annotated fields into controller if their name matches id specified in `fxml` file. – FilipRistic Jul 26 '18 at 16:42