提问人:Ants 提问时间:9/14/2023 更新时间:9/14/2023 访问量:21
NullPointerException - 将参数从一个控制器传递到另一个控制器,接受控制器不接受传递的参数 SQLConnection
NullPointerException - passing parameters from one controller to another the accepting controller does not accept the passed parameter, SQLConnection
问:
javafx.fxml.LoadException:
/C:/Projects/Java/TinyTool-2.4/out/production/TinyTool/views/list/ListDepartments.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2606)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2576)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2446)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2414)
at tinytool.controllers.detail.ControllerUserInfo.showDepartmentList(ControllerUserInfo.java:526)
at tinytool.controllers.detail.ControllerUserInfo.departmentList(ControllerUserInfo.java:163)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
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.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.Scene$ClickGenerator.postProcess(Scene.java:3473)
at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3401)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3769)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3488)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1765)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2497)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
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$2(GlassViewEventHandler.java:434)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:411)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:941)
at com.sun.glass.ui.win.WinApplication._enterNestedEventLoopImpl(Native Method)
at com.sun.glass.ui.win.WinApplication._enterNestedEventLoop(WinApplication.java:214)
at com.sun.glass.ui.Application.enterNestedEventLoop(Application.java:515)
at com.sun.glass.ui.EventLoop.enter(EventLoop.java:107)
at com.sun.javafx.tk.quantum.QuantumToolkit.enterNestedEventLoop(QuantumToolkit.java:634)
at javafx.stage.Stage.showAndWait(Stage.java:478)
at tinytool.utilities.Switcher.showUserInfoScreen(Switcher.java:67)
at tinytool.controllers.ControllerMainScreen.menuUserInfo(ControllerMainScreen.java:140)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
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.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
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.control.MenuItem.fire(MenuItem.java:462)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$6(ContextMenuContent.java:1358)
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.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:3760)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3488)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1765)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2497)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
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$2(GlassViewEventHandler.java:434)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:411)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:941)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:185)
at java.lang.Thread.run(Thread.java:750)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2571)
... 105 more
Caused by: java.lang.NullPointerException
at tinytool.database.server.DAO_Departments.getDepartmentList(DAO_Departments.java:36)
at tinytool.controllers.list.ControllerListDepartments.initialize(ControllerListDepartments.java:55)
... 115 more
org.mariadb.jdbc.Connection@2315b05
连接处于活动状态并连接到 MariaDB 服务器
这是调用类中将显示有效记录列表的方法
private void showDepartmentList() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(TinyTool.class.getResource("/views/list/ListDepartments.fxml"));
AnchorPane pane = loader.load();
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.setTitle("Departments/Areas");
stage.getIcons().add(new Image(tinyTool.ICON + "department.png"));
stage.setWidth(400);
stage.setHeight(360);
Scene scene = new Scene(pane);
stage.setScene(scene);
ControllerListDepartments controller = loader.getController();
controller.setServerConnection(serverConnection, localUserRecord);
stage.showAndWait();
} catch (IOException e) {
e.printStackTrace();
System.out.println(serverConnection);
}
}
输出中的“serverConnection”连接到 MariaDB
这是上述方法调用的控制器,但这里 由于某种原因,serverConnection 为 null 或不被公共方法 setServerConnection 接受
public class ControllerListDepartments {
private final DAO_Departments daoDepartments = new DAO_Departments();
private final Utilities utilities = new Utilities();
private LocalUserRecord record = new LocalUserRecord();
private Connection serverConnection;
@FXML private TableView<DepartmentList> tblDepartments;
@FXML private TableColumn<DepartmentList, String> colName;
@FXML private TableColumn<DepartmentList, String> colCode;
@FXML private JFXTextField txtSearch;
@FXML private Button btnSelect;
@FXML private Button btnClose;
@FXML void initialize() {
colName.prefWidthProperty().bind(tblDepartments.widthProperty().multiply(0.1));
colName.setCellValueFactory(cd -> cd.getValue().deptNameProperty());
colCode.setCellValueFactory(cd -> cd.getValue().deptCodeProperty());
ObservableList<DepartmentList> departmentList =
daoDepartments.getDepartmentList(serverConnection, true);
}
public void setServerConnection(Connection connection, LocalUserRecord userRecord) {
this.record = userRecord;
this.serverConnection = connection;
userRecord.setDepartment(record.getDepartment());
}
}
这是 getDepartmentList 和 connection 参数上报的错误中的 DAO
public class DAO_Departments {
public ObservableList<DepartmentList> getDepartmentList(Connection connection, boolean active) {
String sql;
if (active) {
sql = "SELECT * FROM tinytool.departments WHERE dept_status = 1 ORDER BY dept_name;";
} else {
sql = "SELECT * FROM tinytool.departments ORDER BY dept_name;";
}
try {
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
ObservableList<DepartmentList> departmentList = createDepartmentList(resultSet);
statement.close();
resultSet.close();
return departmentList;
} catch (SQLException e) {
return null;
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="360.0" prefWidth="400.0"
xmlns="http://javafx.com/javafx/8.0.171"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="tinytool.controllers.list.ControllerListDepartments">
<JFXTextField fx:id="txtSearch" layoutX="14.0" layoutY="321.0" promptText="department name" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="5.0" />
<ButtonBar layoutX="186.0" layoutY="305.0" prefHeight="40.0" prefWidth="200.0" AnchorPane.bottomAnchor="5.0" AnchorPane.rightAnchor="5.0">
<buttons>
<Button fx:id="btnSelect" mnemonicParsing="false" text="Select" />
<Button fx:id="btnClose" mnemonicParsing="false" text="Close" />
</buttons>
</ButtonBar>
<TableView fx:id="tblDepartments" layoutX="-41.0" layoutY="-8.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="60.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn fx:id="colName" editable="false" maxWidth="400.0" minWidth="400.0" prefWidth="400.0" text="Department/Area" />
<TableColumn fx:id="colCode" prefWidth="75.0" text="C2" />
</columns>
</TableView>
</AnchorPane>
我错过了一些非常愚蠢的东西,我不知道那是什么。一遍又一遍地查看代码在某种程度上并不能解决问题。如果有人能指出我没有看到的东西,我会很棒。
答: 暂无答案
评论