-1

Here is the code:

if(GridPane.getRowIndex(token1) == middleRow && GridPane.getColumnIndex(token1) == middleCol){
    mainGrid.getChildren().remove(token1);
        } else if(GridPane.getRowIndex(token17) == middleRow && GridPane.getColumnIndex(token17) == middleCol){
                mainGrid.getChildren().remove(token17);
        } else if(GridPane.getRowIndex(token3) == middleRow && GridPane.getColumnIndex(token3) == middleCol){
                mainGrid.getChildren().remove(token3);
        } else if(GridPane.getRowIndex(token4) == middleRow && GridPane.getColumnIndex(token4) == middleCol){
                mainGrid.getChildren().remove(token4);
        } else if(GridPane.getRowIndex(token5) == middleRow && GridPane.getColumnIndex(token5) == middleCol){
                mainGrid.getChildren().remove(token5);
        } else if(GridPane.getRowIndex(token6) == middleRow && GridPane.getColumnIndex(token6) == middleCol){...}

The above code continues similar statements.

Here is the error:

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.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(Thread.java:748)
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:1771)
    ... 31 more
Caused by: java.lang.NullPointerException
    at sudoku.FXMLDocumentController.removeMiddleToken(FXMLDocumentController.java:166)
    at sudoku.FXMLDocumentController.moveToken(FXMLDocumentController.java:162)
    at sudoku.FXMLDocumentController.isMoveLegal(FXMLDocumentController.java:151)
    at sudoku.FXMLDocumentController.square33Click(FXMLDocumentController.java:447)
    ... 41 more

The error starts at the first "else if" statement following the first "if" statement. All of the code seems to make sense but is not working in the running of the program. I do not understand.

Thanks!

Edit Summary: I just want to clarify that my post was not a duplicate of another. At least I don't think the answers to the other post helped me.

Maddog
  • 23
  • 5
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Joe C Dec 31 '18 at 19:04

1 Answers1

2

Assuming it's indeed

GridPane.getRowIndex(token17) == middleRow

or

GridPane.getColumnIndex(token17) == middleCol

causing the issue:

GridPane.getRowIndex and GridPane.getColumnIndex return the reference type Integer. For Nodes that are not assigned a row/column index, null is returned. These nodes may be children of a GridPane nonetheless, using the default row/column of 0.

Using an Integer and an int operand for == results in the same bytecode as calling the intValue() method for the Integer value explicitly. Calling this method on null results in the exception you get.

else if (GridPane.getRowIndex(token17).intValue() == middleRow
    && GridPane.getColumnIndex(token17).intValue() == middleCol) {
    ...
}

Using the following method to convert the Integer to int should solve the issue:

public static int toIndex(Integer value) {
    return value == null ? 0 : value.intValue();
}
else if (toIndex(GridPane.getRowIndex(token17)) == middleRow
    && toIndex(GridPane.getColumnIndex(token17)) == middleCol) {
    ...
}
fabian
  • 80,457
  • 12
  • 86
  • 114