0

I have a record in database which being get must be in the tree table view. Here is the filling method:

 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);
    }

after this method user doesn't see anything in the treeTableView. Maybe there nessessary to define cell content, but this code being put before goodTreeTableView = new TreeTableView<>(rootItem); drops 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()));

I guess this is the reason of my problem but why this drops NPE I can't figure out.

UPD: if I add setting cell value factory I get the following stacktrace:

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: rootItem

Nikitin Mikhail
  • 2,983
  • 9
  • 42
  • 67

1 Answers1

1

You are adding the data to an GUI element on FXML, by re-initializing it. Avoid re-initialization of FXML elements.

You can change the signature of updateGoods() to :

public TreeItem<Good> updateGoods() {
...
}

and use it in the MainController to set items

goodTreeTableView.setRoot(updateGoods());

Preferably, you can also return only data(List) from updateGoods() instead of returning a TreeItem. Create it in the Controller and set it as the root of goodTreeTableView.

ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176