0

Am populating data in table View of javafx tables but am unnecessarily constantly getting a null pointer error on the columns that I surely failed to trace it's cause.

This is the section of the tableview in the Controller:

      private ObservableList<Branches> tableDate= FXCollections.observableArrayList();
@FXML // fx:id="branchId"
private TableColumn<Branches, Integer> BranchId; // Value injected by FXMLLoader
@FXML // fx:id="branchType"
private TableColumn<Branches, Integer> BranchType; // Value injected by FXMLLoader
@FXML // fx:id="branchName"
private TableColumn<Branches, String> BranchName; // Value injected by FXMLLoader
@FXML // fx:id="branchApproval"
private TableColumn<Branches, Integer> BranchApproval; // Value injected by FXMLLoader
@FXML // fx:id="table"
private TableView<Branches> table; // Value injected by FXMLLoader



      @FXML // This method is called by the FXMLLoader when initialization is complete
     void initialize() {
   //        Platform.runLater(()->{
       connect.getBranches().subscribe(branches-> tableDate.add(branches));
       logger.info(tableDate.size()+"=what?");
        if(tableDate.size()>0) {
        tableDate.forEach(items->{logger.info(items.toString());});
        logger.toString();
        BranchId.setCellValueFactory(new PropertyValueFactory<>("branchId"));
        BranchName.setCellValueFactory(new PropertyValueFactory<>("branchName"));
        BranchType.setCellValueFactory(new PropertyValueFactory<>("branchType"));
        BranchApproval.setCellValueFactory(new PropertyValueFactory<> 
      ("fkApprovalDetailsIdBranch"));
            table.setItems(tableDate);
          }
         logger.info(tableDate.size()+"=what?");
    //        });

        }

This is the log of the dataset that am trying to populate:

   +03:00  INFO 14080 --- [lication Thread] c.g.p.c.BackLoginMainController          : 
    Branches(branchId=500, branchType=1100, branchName=MAKINDYE III, 
  fkApprovalDetailsIdBranch=152)
 2022-07-10T10:01:55.643+03:00  INFO 14080 --- [lication Thread] 
   c.g.p.c.BackLoginMainController          : Branches(branchId=501, branchType=1200, 
   branchName=PEAKCOCK HOUSE II, fkApprovalDetailsIdBranch=4)
  2022-07-10T10:01:55.643+03:00  INFO 14080 --- [lication Thread] 
 c.g.p.c.BackLoginMainController          : Branches(branchId=502, branchType=1200, 
 branchName=KIWANDAGALALA, fkApprovalDetailsIdBranch=139)
 2022-07-10T10:01:55.643+03:00  INFO 14080 --- [lication Thread] 
 c.g.p.c.BackLoginMainController          : Branches(branchId=503, branchType=1200, 
 branchName=BRANCH TYPE, fkApprovalDetailsIdBranch=140)
 2022-07-10T10:01:55.643+03:00  INFO 14080 --- [lication Thread] 
 c.g.p.c.BackLoginMainController          : Branches(branchId=504, branchType=1200, 
 branchName=BRANCH 4, fkApprovalDetailsIdBranch=141)
2022-07-10T10:01:55.643+03:00  INFO 14080 --- [lication Thread] 
c.g.p.c.BackLoginMainController          : Branches(branchId=505, branchType=1200, 
branchName=BRANCH 9, fkApprovalDetailsIdBranch=142)
2022-07-10T10:01:55.643+03:00  INFO 14080 --- [lication Thread] 
c.g.p.c.BackLoginMainController          : Branches(branchId=506, branchType=1100, 
branchName=KATOSI 11, fkApprovalDetailsIdBranch=150)
2022-07-10T10:01:55.643+03:00  INFO 14080 --- [lication Thread] 
 c.g.p.c.BackLoginMainController          : Branches(branchId=508, branchType=1100, 
 branchName=NANSANA BRANCH, fkApprovalDetailsIdBranch=212)
 2022-07-10T10:01:55.643+03:00  INFO 14080 --- [lication Thread] 
  c.g.p.c.BackLoginMainController          : Branches(branchId=509, branchType=1200, 
 branchName=GULU, fkApprovalDetailsIdBranch=213)
 2022-07-10T10:01:55.643+03:00  INFO 14080 --- [lication Thread] 
 c.g.p.c.BackLoginMainController          : Branches(branchId=510, branchType=1100, 
  branchName=NTINDA, fkApprovalDetailsIdBranch=214)
2022-07-10T10:01:55.643+03:00  INFO 14080 --- [lication Thread] 
 c.g.p.c.BackLoginMainController          : Branches(branchId=511, branchType=1200, 
branchName=NEW BRANCH TESTING, fkApprovalDetailsIdBranch=215)
2022-07-10T10:01:55.643+03:00  INFO 14080 --- [lication Thread] 
c.g.p.c.BackLoginMainController          : Branches(branchId=512, branchType=1100, 
 branchName=NEW HEADOFFICE, fkApprovalDetailsIdBranch=216)
 2022-07-10T10:01:55.643+03:00  INFO 14080 --- [lication Thread] 
 c.g.p.c.BackLoginMainController          : Branches(branchId=513, branchType=1100, 
 branchName=NEW HEADOFFICESS, fkApprovalDetailsIdBranch=217)
 2022-07-10T10:01:55.643+03:00  INFO 14080 --- [lication Thread] 
 c.g.p.c.BackLoginMainController          : Branches(branchId=514, branchType=1200, 
 branchName=NEW KATWE BRANCH, fkApprovalDetailsIdBranch=218)
 2022-07-10T10:01:55.644+03:00  INFO 14080 --- [lication Thread] 
 c.g.p.c.BackLoginMainController          : Branches(branchId=515, branchType=1100, 
 branchName=MATUGA BRANCH, fkApprovalDetailsIdBranch=221)

This is the full error log:

       Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1857)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1724)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
    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$ClickGenerator.postProcess(Scene.java:3586)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3890)
    at javafx.scene.Scene.processMouseEvent(Scene.java:1874)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2607)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:551)
    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$runLoop$3(WinApplication.java:184)
    at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
    at java.base/java.lang.reflect.Method.invoke(Method.java:577)
    at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:77)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:577)
    at com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275)
    at com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1852)
    ... 29 more
Caused by: java.lang.RuntimeException: javafx.fxml.LoadException: 
/E:/Googo%20Projects/PROJECTS/pinkApple/pinkAppleFrontEnd/FrontEnd/pinkappleGui/target/classes/views/main/main.fxml

    at com.gui.pinkapplegui.StageInitializer.createView(StageInitializer.java:52)
    at com.gui.pinkapplegui.controllers.LoginControler.loginToNewWindow(LoginControler.java:37)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    ... 36 more
Caused by: javafx.fxml.LoadException: 
/E:/Googo%20Projects/PROJECTS/pinkApple/pinkAppleFrontEnd/FrontEnd/pinkappleGui/target/classes/views/main/main.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2707)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2677)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2516)
    at com.gui.pinkapplegui.StageInitializer.createView(StageInitializer.java:48)
    ... 38 more
    Caused by: java.lang.reflect.InvocationTargetException
        at 
 
java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:116)
        at java.base/java.lang.reflect.Method.invoke(Method.java:577)
        at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:77)
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
        at java.base/java.lang.reflect.Method.invoke(Method.java:577)
        at com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275)
        at com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2673)
        ... 41 more
   

 Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.control.TableColumn.setCellValueFactory(javafx.util.Callback)" because "this.BranchId" is null
        at com.gui.pinkapplegui.controllers.BackLoginMainController.initialize(BackLoginMainController.java:72)
        at 
     
     java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104

)
        

This is the FXML file:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.Pane?>

<Pane maxHeight="749.0" maxWidth="1000.0" minHeight="700.0" minWidth="992.0" prefHeight="749.0" prefWidth="992.0" style="-fx-background-color: #2e2d2d;" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.gui.pinkapplegui.controllers.BackLoginMainController">
   <children>
      <Button layoutX="398.0" layoutY="698.0" mnemonicParsing="false" onMouseClicked="#getbackToLogIn" prefHeight="25.0" prefWidth="152.0" style="-fx-background-color: #c9104e;" text="Back" textFill="WHITE" />
      <Pane layoutX="23.0" layoutY="14.0" prefHeight="669.0" prefWidth="901.0" style="-fx-background-color: #c9104e;">
         <children>
            <TableView layoutX="25.0" layoutY="38.0" prefHeight="592.0" prefWidth="854.0">
              <columns>
                <TableColumn fx:id="branchId" prefWidth="122.0" text="BranchId" />
                <TableColumn fx:id="branchName" prefWidth="365.0" text="BranchName" />
                  <TableColumn fx:id="branchType" minWidth="0.0" prefWidth="130.0" text="BranchType" />
                  <TableColumn fx:id="branchApproval" prefWidth="189.0" text="BranchApproval" />
              </columns>
            </TableView>
         </children>
      </Pane>
   </children>
</Pane>
Googo
  • 79
  • 1
  • 8
  • 1
    Case matters: `branchId != BranchId`. Note the former matches standard Java naming conventions, whereas the latter does not. – Slaw Jul 10 '22 at 07:35
  • I thought the column names do not necessarily have to follow the naming conventions. That is not the issue though because i changed the case but still brings the same issue – Googo Jul 10 '22 at 07:38
  • 1
    The column names can be whatever you want. The **field** names _should_ follow standard Java naming conventions, and _must_ match the `fx:id` attribute value exactly. – Slaw Jul 10 '22 at 07:39
  • 1
    e.g., `@FXML private TableColumn branchId;` instead of what you currently have. – Slaw Jul 10 '22 at 07:41
  • I have made those adjustments but now the error has pointed to tableview object:Cannot invoke "javafx.scene.control.TableView.setItems(javafx.collections.ObservableList)" because "this.table" is null – Googo Jul 10 '22 at 08:34
  • missing fx:id for table - unrelated: don't hard-code locations nor sizing constraints for nodes, instead use a suitable layout pane – kleopatra Jul 10 '22 at 09:32
  • Thanks so much guys, it now working perfectly. Thanks @kleopatra for that extra advice, i was beginning to see some issues with my layouts – Googo Jul 10 '22 at 16:31
  • Am wondering whether there is a way the above can be provided as answer and we vote it so that it can help someone else in the same situation, small as it is it saw me spend sleepless nights – Googo Jul 10 '22 at 16:32
  • @Googo for an answer to be provided, the question would need to be reopened (which takes a few eligible users voting to do) and somebody would need to create and submit an answer. Likely there won't be enough reopen votes, so it won't be reopened. The prospective answer would, in any case, be a duplicate of an existing answer (I don't know which one without looking, but am sure there is one). Preferably the question would have been closed as a duplicate but it wasn't. To close as a duplicate it would need to be reopened then closed for a different reason, which I don't think will happen. – jewelsea Jul 11 '22 at 23:30

0 Answers0