我在数据库中有一条记录,必须在树表视图中。这是填充方法:
private void updateGoods(){
goodsPane.setCenter(goodTreeTableView);
List<Good> goodAndFoldersList;
try {
goodAndFoldersList = goodsService.getGoods();//database query
}catch (SQLException e){
goodAndFoldersList = new ArrayList<>();
log.log(Level.ERROR, e.getMessage());
}
List<Good> goods = new ArrayList<Good>();//list for roots
List<Good> roots = new ArrayList<Good>();//list for goods themselves
for (Good good : goodAndFoldersList) {
if (good.isIs_folder()) {
roots.add(good);
} else {
goods.add(good);
}
}
TreeItem<Good> rootItem = new TreeItem<>();//the main root
for (Good root : roots) {
Long folderId = root.getId();
TreeItem<Good> rootTreeItem = new TreeItem<>(root);
for (Good good : goods) {//looking for goods for each folder
if (good.getFolderId() == folderId) {
TreeItem<Good> goodTreeItem = new TreeItem<>(good);
rootTreeItem.getChildren().add(goodTreeItem);
}
}
rootItem.getChildren().add(rootTreeItem);//adding every folder to the main root
}
goodTreeTableView = new TreeTableView<>(rootItem);
}
在此方法之后,用户在treeTableView中看不到任何内容。也许有必要定义单元格内容,但这个代码放在goodTreeTableView = new TreeTableView<>(rootItem);
之前会丢弃NullPointerException:
goodName.setCellValueFactory((TreeTableColumn.CellDataFeatures<Good, String> param) ->
new ReadOnlyStringWrapper(param.getValue().getValue().getName()));
folderId.setCellValueFactory((TreeTableColumn.CellDataFeatures<Good, Number> param) ->
new ReadOnlyLongWrapper(param.getValue().getValue().getFolderId()));
is_folder.setCellValueFactory((TreeTableColumn.CellDataFeatures<Good, Boolean> param) ->
new ReadOnlyBooleanWrapper(param.getValue().getValue().isIs_folder()));
我想这就是我的问题的原因,但为什么这会导致NPE
我无法理解。
UPD:如果我添加设置单元格值工厂,我会得到以下堆栈跟踪:
javafx.fxml.LoadException:
/D:/javaEdi/linkserver/ediagent/target/classes/com/ediagent/edi/gui/Main.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2595)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2565)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2435)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2403)
at com.ediagent.edi.gui.Main.initRootLayout(Main.java:44)
at com.ediagent.edi.gui.Main.start(Main.java:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$51/1546859350.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$44/1712669532.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/157858792.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/1225373914.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/152005629.run(Unknown Source)
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:483)
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:483)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2560)
... 18 more
Caused by: java.lang.NullPointerException
at com.ediagent.edi.gui.MainController.updateGoods(MainController.java:309)
at com.ediagent.edi.gui.MainController.initialize(MainController.java:328)
... 28 more
** UPD:** rootItem:
答案 0 :(得分:1)
您正在通过重新初始化将数据添加到FXML上的GUI元素。避免重新初始化FXML元素。
您可以将updateGoods()
的签名更改为:
public TreeItem<Good> updateGoods() {
...
}
并在MainController
中使用它来设置项目
goodTreeTableView.setRoot(updateGoods());
最好还可以从updateGoods()
返回数据(List),而不是返回TreeItem。在Controller中创建它并将其设置为goodTreeTableView
的根目录。