title: I want to get GridPane's index when I act click event
body: <p>I want to get GridPane's index when I act MouseClickEvent.
  I made clickevent on AnchorPane in GridPane.
  I succeed to change mainmenu.fxml to koreanfood.fxml on click.
  But I want to get GridPane's index 
  because I want to do Same thing on one .fxml
  when i click on another AnchorPane in GridPane.</p>
  
  <p>I tried <a href="https://stackoverflow.com/questions/20825935/javafx-get-node-by-row-and-column/20828724#20828724">this method</a> and <a href="https://stackoverflow.com/questions/50012463/how-can-i-click-a-gridpane-cell-and-have-it-perform-an-action">this method</a> and <a href="https://stackoverflow.com/questions/39178981/accessing-the-column-and-row-index-of-gridpane-in-javafx-keeps-returning-null">this method</a></p>
  
  <p>But getRowIndex()and getColumnIndex() always returned null</p>
  
  <p>Main.java:</p>
  
  <pre><code>package application;
  
  import javafx.application.Application;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.stage.Stage;
  
  
  public class Main extends Application {
      @Override
      public void start(Stage primaryStage) {
          try {
              Parent root=FXMLLoader.load(getClass().getResource("mainmenu.fxml"));
              primaryStage.setScene(new Scene(root));         
              primaryStage.setTitle("recipe");
              primaryStage.show();
  //          primaryStage.setResizable(false);
          } catch(Exception e) {
              e.printStackTrace();
          }
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>
  
  <p>Controller.java:</p>
  
  <pre><code>package application;
  
  import javafx.fxml.FXML;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Node;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.scene.control.Label;
  import javafx.scene.input.MouseEvent;
  import javafx.scene.layout.GridPane;
  import javafx.stage.Stage;
  
  public class Controller {
      @FXML
      private Label food;
      @FXML
      private void clickmainmenu(MouseEvent event) {
  
          try {
              Parent koreanfood = FXMLLoader.load(getClass().getResource("koreanfood.fxml"));
              Scene scene = new Scene(koreanfood);
              Stage primaryStage = (Stage)((Node) event.getSource()).getScene().getWindow();
  
              Integer row = GridPane.getRowIndex((Node)event.getSource() );
              int column=GridPane.getColumnIndex((Node)event.getSource() );
              System.out.println("r : "+row+"c : "+column);
              if(row==0) {
                  switch(column) {
                  case 0:
                      food.setText("한식");
                      break;
                  case 1:
                      food.setText("중식");
                      break;
                  case 2:
                      food.setText("일식");
                      break;
                  case 3:
                      food.setText("양식");
                      break;      
                  }
              }
              else if(row==1) {
                  switch(column) {
                  case 0:
                      food.setText("분식");
                      break;
                  case 1:
                      food.setText("야식");
                      break;
                  case 2:
                      food.setText("랜덤");
                      break;
                  case 3:
                      food.setText("ㅋ");
                      break;      
                  }
              }           
  
              primaryStage.setScene(scene);
              primaryStage.setTitle("recipe");
          } catch(Exception e) {
              e.printStackTrace();
          }
  
      }
      @FXML
      private void clickfoodmenu(MouseEvent event) {
          try {
              Parent cookingrecipe = FXMLLoader.load(getClass().getResource("cookingrecipe.fxml"));
              Scene scene = new Scene(cookingrecipe);
  
              Stage primaryStage = (Stage)((Node) event.getSource()).getScene().getWindow();
  
              primaryStage.setScene(scene);
              primaryStage.setTitle("recipe");
          } catch(Exception e) {
              e.printStackTrace();
          }
      }
      @FXML
      private void clickBackMain(MouseEvent event) {
          try {
              Parent koreanfood = FXMLLoader.load(getClass().getResource("mainmenu.fxml"));
              Scene scene = new Scene(koreanfood);
  
              Stage primaryStage = (Stage)((Node) event.getSource()).getScene().getWindow();
  
              primaryStage.setScene(scene);
              primaryStage.setTitle("recipe");
          } catch(Exception e) {
              e.printStackTrace();
          }
      }
      @FXML
      private void clickBackKor(MouseEvent event) {
          try {
              Parent mainmenu = FXMLLoader.load(getClass().getResource("koreanfood.fxml"));
              Scene scene = new Scene(mainmenu);
  
              Stage primaryStage = (Stage)((Node) event.getSource()).getScene().getWindow();
  
              primaryStage.setScene(scene);
              primaryStage.setTitle("recipe");
          } catch(Exception e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>mainmenu.fxml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import javafx.scene.text.*?&gt;
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import javafx.scene.image.*?&gt;
  &lt;?import java.lang.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  &lt;?import javafx.scene.layout.AnchorPane?&gt;
  
  &lt;AnchorPane prefHeight="600.0" prefWidth="800.0" style="-fx-background-color: yellow;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller"&gt;
     &lt;children&gt;
        &lt;GridPane hgap="30.0" layoutX="70.0" layoutY="200.0" prefHeight="320.0" prefWidth="682.0" vgap="30.0"&gt;
          &lt;columnConstraints&gt;
              &lt;ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /&gt;
              &lt;ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /&gt;
            &lt;ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /&gt;
            &lt;ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /&gt;
          &lt;/columnConstraints&gt;
          &lt;rowConstraints&gt;
            &lt;RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /&gt;
            &lt;RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /&gt;
          &lt;/rowConstraints&gt;
           &lt;children&gt;
              &lt;AnchorPane onMouseClicked="#clickmainmenu" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;"&gt;
                 &lt;children&gt;
                    &lt;ImageView fitHeight="101.0" fitWidth="103.0" layoutX="27.0" layoutY="7.0" pickOnBounds="true" preserveRatio="true"&gt;
                       &lt;image&gt;
                          &lt;Image url="@../../../resources/iconfinder_Food_C226_2427886.png" /&gt;
                       &lt;/image&gt;&lt;/ImageView&gt;
                    &lt;Label layoutX="54.0" layoutY="119.0" text="한식"&gt;
                       &lt;font&gt;
                          &lt;Font size="21.0" /&gt;
                       &lt;/font&gt;&lt;/Label&gt;
                 &lt;/children&gt;
              &lt;/AnchorPane&gt;
              &lt;AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1"&gt;
                 &lt;children&gt;
                    &lt;Button layoutY="1.0" mnemonicParsing="false" onMouseClicked="#clickmainmenu" prefHeight="123.0" prefWidth="119.0" style="-fx-background-color: white;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" /&gt;
                    &lt;ImageView fitHeight="114.0" fitWidth="116.0" layoutX="14.0" pickOnBounds="true" preserveRatio="true"&gt;
                       &lt;image&gt;
                          &lt;Image url="@../../../resources/dim-sum.png" /&gt;
                       &lt;/image&gt;
                    &lt;/ImageView&gt;
                    &lt;Label layoutX="54.0" layoutY="119.0" text="중식"&gt;
                       &lt;font&gt;
                          &lt;Font size="21.0" /&gt;
                       &lt;/font&gt;
                    &lt;/Label&gt;
                 &lt;/children&gt;
              &lt;/AnchorPane&gt;
              &lt;AnchorPane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;" GridPane.columnIndex="2"&gt;
                 &lt;children&gt;
                    &lt;Label layoutX="51.0" layoutY="121.0" text="일식"&gt;
                       &lt;font&gt;
                          &lt;Font size="21.0" /&gt;
                       &lt;/font&gt;
                    &lt;/Label&gt;
                    &lt;ImageView fitHeight="128.0" fitWidth="122.0" layoutX="13.0" pickOnBounds="true" preserveRatio="true"&gt;
                       &lt;image&gt;
                          &lt;Image url="@../../../resources/iconfinder_Food_C212_2427856.png" /&gt;
                       &lt;/image&gt;
                    &lt;/ImageView&gt;
                 &lt;/children&gt;
              &lt;/AnchorPane&gt;
              &lt;AnchorPane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;" GridPane.columnIndex="3"&gt;
                 &lt;children&gt;
                    &lt;ImageView fitHeight="114.0" fitWidth="114.0" layoutX="17.0" pickOnBounds="true" preserveRatio="true" /&gt;
                    &lt;Label layoutX="51.0" layoutY="121.0" text="양식"&gt;
                       &lt;font&gt;
                          &lt;Font size="21.0" /&gt;
                       &lt;/font&gt;
                    &lt;/Label&gt;
                 &lt;/children&gt;&lt;/AnchorPane&gt;
              &lt;AnchorPane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;" GridPane.rowIndex="1"&gt;
                 &lt;children&gt;
                    &lt;ImageView fitHeight="114.0" fitWidth="114.0" layoutX="17.0" pickOnBounds="true" preserveRatio="true" /&gt;
                    &lt;Label layoutX="51.0" layoutY="114.0" text="분식"&gt;
                       &lt;font&gt;
                          &lt;Font size="21.0" /&gt;
                       &lt;/font&gt;
                    &lt;/Label&gt;
                 &lt;/children&gt;&lt;/AnchorPane&gt;
              &lt;AnchorPane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;" GridPane.columnIndex="1" GridPane.rowIndex="1"&gt;
                 &lt;children&gt;
                    &lt;ImageView fitHeight="114.0" fitWidth="114.0" layoutX="21.0" pickOnBounds="true" preserveRatio="true" /&gt;
                    &lt;Label layoutX="51.0" layoutY="114.0" text="야식"&gt;
                       &lt;font&gt;
                          &lt;Font size="21.0" /&gt;
                       &lt;/font&gt;
                    &lt;/Label&gt;
                 &lt;/children&gt;&lt;/AnchorPane&gt;
              &lt;AnchorPane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;" GridPane.columnIndex="2" GridPane.rowIndex="1"&gt;
                 &lt;children&gt;
                    &lt;ImageView fitHeight="114.0" fitWidth="114.0" layoutX="17.0" pickOnBounds="true" preserveRatio="true" /&gt;
                    &lt;Label layoutX="51.0" layoutY="114.0" text="랜덤"&gt;
                       &lt;font&gt;
                          &lt;Font size="21.0" /&gt;
                       &lt;/font&gt;
                    &lt;/Label&gt;
                 &lt;/children&gt;&lt;/AnchorPane&gt;
              &lt;AnchorPane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;" GridPane.columnIndex="3" GridPane.rowIndex="1"&gt;
                 &lt;children&gt;
                    &lt;ImageView fitHeight="114.0" fitWidth="130.0" layoutX="11.0" pickOnBounds="true" preserveRatio="true"&gt;
                       &lt;image&gt;
                          &lt;Image url="@../../../resources/food.png" /&gt;
                       &lt;/image&gt;&lt;/ImageView&gt;
                    &lt;Label layoutX="10.0" layoutY="119.0" text="너만의 레시피"&gt;
                       &lt;font&gt;
                          &lt;Font size="21.0" /&gt;
                       &lt;/font&gt;
                    &lt;/Label&gt;
                 &lt;/children&gt;&lt;/AnchorPane&gt;
           &lt;/children&gt;
        &lt;/GridPane&gt;
        &lt;Label layoutX="300.0" layoutY="47.0" prefHeight="94.0" prefWidth="220.0" text="뭐먹어"&gt;
           &lt;font&gt;
              &lt;Font size="72.0" /&gt;
           &lt;/font&gt;
        &lt;/Label&gt;
     &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  
  <p>koreanfood.fxml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import javafx.scene.image.*?&gt;
  &lt;?import javafx.scene.text.*?&gt;
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import java.lang.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  &lt;?import javafx.scene.layout.AnchorPane?&gt;
  
  &lt;AnchorPane prefHeight="600.0" prefWidth="800.0" style="-fx-background-color: yellow;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller"&gt;
     &lt;children&gt;
        &lt;Label fx:id="food" layoutX="327.0" layoutY="14.0" prefHeight="99.0" prefWidth="146.0"&gt;
           &lt;font&gt;
              &lt;Font size="70.0" /&gt;
           &lt;/font&gt;
        &lt;/Label&gt;
        &lt;GridPane hgap="20.0" layoutX="61.0" layoutY="121.0" prefHeight="448.0" prefWidth="678.0" vgap="20.0"&gt;
          &lt;columnConstraints&gt;
            &lt;ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /&gt;
            &lt;ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /&gt;
              &lt;ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /&gt;
              &lt;ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /&gt;
          &lt;/columnConstraints&gt;
          &lt;rowConstraints&gt;
            &lt;RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /&gt;
            &lt;RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /&gt;
              &lt;RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /&gt;
          &lt;/rowConstraints&gt;
           &lt;children&gt;
              &lt;AnchorPane onMouseClicked="#clickfoodmenu" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;"&gt;
                 &lt;children&gt;
                    &lt;ImageView fitHeight="99.0" fitWidth="121.0" layoutX="17.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true" /&gt;
                    &lt;Label layoutX="54.0" layoutY="104.0" prefHeight="29.0" prefWidth="58.0" text="111" /&gt;
                 &lt;/children&gt;
              &lt;/AnchorPane&gt;
              &lt;AnchorPane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;" GridPane.rowIndex="1" /&gt;
              &lt;AnchorPane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;" GridPane.rowIndex="2" /&gt;
              &lt;AnchorPane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;" GridPane.columnIndex="1" /&gt;
           &lt;/children&gt;
        &lt;/GridPane&gt;
        &lt;ImageView fitHeight="72.0" fitWidth="132.0" layoutX="32.0" layoutY="24.0" onMouseClicked="#clickBackMain" pickOnBounds="true" preserveRatio="true"&gt;
           &lt;image&gt;
              &lt;Image url="@../../../resources/reply%20(2).png" /&gt;
           &lt;/image&gt;
        &lt;/ImageView&gt;
     &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  
  <p>error:</p>
  
  <pre><code>java.lang.NullPointerException
      at application.Controller.clickmainmenu(Controller.java:25)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at sun.reflect.misc.Trampoline.invoke(Unknown Source)
      at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
      at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
      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.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:3470)
      at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
      at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
      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:394)
      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$353(GlassViewEventHandler.java:432)
      at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
      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$147(WinApplication.java:177)
      at java.lang.Thread.run(Unknown Source)
  </code></pre>
  
  <p>What should I do?
  Can anybody help me??</p>