我们有两个窗口:
第一个表示包含一些数据库数据的tableview,以及3个按钮(添加,删除,刷新)
单击添加按钮时会调用第二个窗口:这里有一些文本字段可以将数据保存到数据库中。
现在问题是我想在第二个窗口中单击“提交”按钮后刷新tableview(在第一个窗口中),这样我就可以在表中看到这些更改,我试图从中创建一个对象第一个类然后调用refresh方法,但它给了我java.lang.NullPointerException。
这是第一个包含tableview
的控制器public class DeptControl implements Initializable {
@Override
public void initialize(URL url, ResourceBundle rb) {
deptid.setCellValueFactory(new PropertyValueFactory<>("DeptNo"));
deptid.setEditable(false);
deptname.setCellValueFactory(new PropertyValueFactory<>("DeptName"));
deptname.setCellFactory(TextFieldTableCell.forTableColumn());
deptlocation.setCellValueFactory(new PropertyValueFactory<>("Location"));
deptlocation.setCellFactory(TextFieldTableCell.forTableColumn());
table.setTableMenuButtonVisible(true);
filltable(); // THIS IS THE REFRESH METHODE
}
int id;
String DEPTNAME;
String DEPTLOCATION;
public void set(){
id =table.getSelectionModel().getSelectedItem().getDeptNo();
DEPTNAME = table.getSelectionModel().getSelectedItem().getDeptName();
DEPTLOCATION = table.getSelectionModel().getSelectedItem().getLocation();
}
public void filltable(){
Department dep = new Department();
dep.GetAllRows(table);
}
现在是第二个控制器java(添加窗口)
public class DeptAddControl implements Initializable{
@Override
public void initialize(URL location, ResourceBundle resources) {
clear();
}
private void clear(){
txtDeptID.setText(new Department().AutoNumber());
txtDeptName.clear();
txtLocation.clear();
txtDeptName.requestFocus();
}
@FXML
private void submit(ActionEvent event) {
int num = (Integer.parseInt(txtDeptID.getText()));
String name = (txtDeptName.getText());
String location = (txtLocation.getText());
Department e = new Department(num,name,location);
btnback.fire();
e.add();
/// Now the data is added and I want to call filltable() from the other class
DeptControl dp = new DeptControl();
dp.filltable(); //I tried this but gives Exception
}
@FXML
private void back(ActionEvent event) {
Stage stage = (Stage)btnback.getScene().getWindow();
stage.close();
}
}
异常
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
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.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.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:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
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(Thread.java:745)
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$MethodHandler.invoke(FXMLLoader.java:1769)
... 52 more
Caused by: java.lang.NullPointerException
at Entity.Department.GetAllRows(Department.java:125)
at controllers.DeptControl.filltable(DeptControl.java:119)
at controllers.DeptAddControl.submit(DeptAddControl.java:68)
... 62 more
抛出异常的GetAllRows()
@Override
public void GetAllRows(TableView table) {
ObservableList<Department> list = FXCollections.observableArrayList();
ResultSet rs = go.FillTable("department_data");
try{
while (rs.next()){
list.add(new Department( rs.getInt(1) , rs.getString(2), rs.getString(3)));
}
con.close();
table.setItems(list);
}catch(SQLException e){
tools.Notification_Error("Error", e.getMessage(), Pos.CENTER);
}
}
这是Filltable()
public static ResultSet FillTable(String TableNameOrStatement) {
try {
connect();
Statement s = con.createStatement();
String Query;
if("select ".equals(TableNameOrStatement.substring(0, 7).toLowerCase())){
Query = TableNameOrStatement;
}
else {
Query = "select * from " + TableNameOrStatement;
}
ResultSet rs = s.executeQuery(Query);
return rs;
} catch (SQLException ex) {
tools.Notification_Error("Error", ex.getMessage(), Pos.CENTER);
return null;
}
}
答案 0 :(得分:-1)
DeptControl.filltable();
可能是其他问题。