Search code examples
javajavafxnullpointerexception

JavaFX load exception while create choiceBox


I have a little problem when i load controller and i want to create some choicebox. I create new stage and i want to load scene with maincontroller object.

javafx.fxml.LoadException: 
/C:/Users/Kutti/workspace/SimplyGoals/bin/simplygoals/view/RemoveUser.fxml

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at simplygoals.control.MainPanelController.lambda$2(MainPanelController.java:106)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at simplygoals.control.RemoveUserController.choiceBoxInit(RemoveUserController.java:45)
at simplygoals.control.RemoveUserController.initialize(RemoveUserController.java:39)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 50 more

There is controller:

 package simplygoals.control;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import simplygoals.modelLogicComponents.User;
import simplygoals.control.MainPanelController;

public class RemoveUserController extends MainController implements Initializable {
private MainPanelController mainControl;
@FXML
private ChoiceBox<User> UsersChoiceBox;

@FXML
private Label MessageLabel;

@FXML
private Button RemoveUsersButton;

@FXML
void f50303(ActionEvent event) {

}
boolean userRemoved=false;
User removedUser=null;
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        choiceBoxInit();
    //  observeChoiceBox();
    //  removeUser();
    }

    public void choiceBoxInit(){
        System.out.println(this.mainControl.getModelLogic());
    //  UsersChoiceBox.setItems(this.mainControl.getModelLogic().getUserList());
    }
    public void observeChoiceBox(){
        UsersChoiceBox.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>(){
        public void changed(ObservableValue ov, Number value, Number new_value){
                    if((new_value.intValue()>=0)&&(new_value.intValue()<=mainControl.getModelLogic().getUserList().size())){
                        removedUser=mainControl.getModelLogic().getUserList().get(new_value.intValue());
                    }
        }
        });
    }
    public void removeUser(){
        RemoveUsersButton.setTooltip(new Tooltip("Select user"));
        RemoveUsersButton.setOnAction(x->{

            String message = mainControl.getModelLogic().removeUserFromLogic(removedUser);
            MessageLabel.setText("");               
            UsersChoiceBox.getItems().clear();
            UsersChoiceBox.setItems(mainControl.getModelLogic().getUserList());
            mainControl.updateComboBox();
        });
    }

    public void setMainControl(MainPanelController mainPanel){
        mainControl=mainPanel;
    }

}

Here is what i imported in my xml file:

<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

Thank you for help

Main Panel Controller class:

public class MainPanelController extends MainController implements Initializable {

@FXML
private TopPanelController topPanelController;
public TopPanelController getTopPanelController() {
    return topPanelController;
}

@FXML
private MenuPanelController menuPanelController;

@FXML
private LeftPanelTimeModeController leftPanelTimeModeController;

@FXML
private CenterPanelTableController centerPanelTableController;

@Override
public void initialize(URL location, ResourceBundle resources) {

    getModelLogic().createDB();//if there is no database create it, otherwise not
    getModelLogic().updateUsers();//copy users from database to ModelLogic
    updateComboBox();
    showAddUserPanel();
    showRemoveUserPanel();
    setUser();
    modelLogicToFile();

}
public void updateComboBox(){
    ComboBox<String> comboBox = getTopPanelController().getUsersComboBox();

    comboBox.setOnMouseClicked(event->{
        comboBox.setItems(getModelLogic().getUsersObservableList());        
    });
}
public void setUser(){
    ComboBox<String> comboBox = getTopPanelController().getUsersComboBox();
    comboBox.valueProperty().addListener(new ChangeListener<String>() {
        @Override 
        public void changed(ObservableValue ov, String v1, String v2) {                
            if((v2!=null)){getModelLogic().setCurrentUser(v2);}


        }    
    });
}
public void showAddUserPanel(){
    Button button = getTopPanelController().getAddUserButton();
    button.setOnAction(event ->  {
                        try {
                            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/simplygoals/view/AddUser.fxml"));
                            Parent root1 = (Parent) fxmlLoader.load();
                            Stage stage = new Stage();
                            stage.setResizable(false);
                            stage.setTitle("Add User");
                            stage.setScene(new Scene(root1));  
                            AddUserController addUserController = fxmlLoader.getController();
                            addUserController.setMainControl(this);
                            stage.show();
                        } catch(Exception e) {
                            e.printStackTrace();
                        }
                                });
}
public void showRemoveUserPanel(){
    Button button = getTopPanelController().getRemoveUserButton();
    button.setOnAction(event ->  {
                        try {
                            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/simplygoals/view/RemoveUser.fxml"));
                            Parent root2 = (Parent) fxmlLoader.load();
                            Stage stage = new Stage();
                            stage.setResizable(false);
                            stage.setTitle("Remove User");
                            stage.setScene(new Scene(root2));
                            RemoveUserController removeUserController = fxmlLoader.getController();
                            removeUserController.setMainControl(this);
                            stage.show();
                        } catch(Exception e) {
                            e.printStackTrace();
                        }
                                });

}

}


Solution

  • This is nothing more than the standard debugging of a null pointer exception.

    As you can plainly see from the stack trace, mainControl is null when RemoveUserController.choiceBoxInit() is called. That method is called from RemoveUserController.initialize().

    The initialize() method in a controller is called when the FXMLLoader loads the FXML file - in other words it is called from the FXMLLoader.load(...) method.

    You do not assign a value to mainControl until you call the setMainController(...) method, which (obviously) you do after load() has finished:

    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/simplygoals/view/RemoveUser.fxml"));
    
    // this line causes RemoveUserController.initialize() to be called:
    Parent root2 = (Parent) fxmlLoader.load();
    
    Stage stage = new Stage();
    stage.setResizable(false);
    stage.setTitle("Remove User");
    stage.setScene(new Scene(root2));
    RemoveUserController removeUserController = fxmlLoader.getController();
    
    // mainControl is not initialized in removeUserController until you do this:
    removeUserController.setMainControl(this);
    

    Therefore, you cannot access mainControl until setMainControl is called. Just move the call to choiceBoxInit() from initialize() to setMainControl():

    @Override
    public void initialize(URL location, ResourceBundle resources) {
    //  choiceBoxInit();
    //  observeChoiceBox();
    //  removeUser();
    }
    
    public void choiceBoxInit(){
        System.out.println(this.mainControl.getModelLogic());
    //  UsersChoiceBox.setItems(this.mainControl.getModelLogic().getUserList());
    }
    
    // ...
    
    public void setMainControl(MainPanelController mainPanel){
        mainControl=mainPanel;
        choiceBoxInit();
    }