当我加载控制器并且我想创建一些选择框时,我有一点问题。 我创建了新的舞台,我想用maincontroller对象加载场景。
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
有控制器:
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;
}
}
以下是我在xml文件中导入的内容:
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
感谢您的帮助
主面板控制器类:
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();
}
});
}
}
答案 0 :(得分:1)
这只不过是空指针异常的标准调试。
从stack trace可以清楚地看到,mainControl
在调用RemoveUserController.choiceBoxInit()
时为空。该方法是从RemoveUserController.initialize()
调用的。
当initialize()
加载FXML文件时调用控制器中的FXMLLoader
方法 - 换句话说,它是从FXMLLoader.load(...)
方法调用的。
在调用mainControl
方法之前,您没有为setMainController(...)
分配值,这显然是在 load()
完成后 p>
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);
因此,在调用mainControl
之前,您无法访问setMainControl
。只需将choiceBoxInit()
的通话从initialize()
移至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();
}