0

I have a project in school, where I need to do a 15 puzzle game in javaFX using the Controller and scene builder. I have a problem beacause I want to use a grid pane to put my picture in it and using the index to change position. Here is my code :

 @FXML
private Button ouvrirTaquin;
@FXML
private GridPane gridPane;
@FXML
private ImageView c1;
@FXML
private ImageView c2;
@FXML
private ImageView c3;
@FXML
private ImageView c4;
@FXML
private ImageView c5;
@FXML
private ImageView c6;
@FXML
private ImageView c7;
@FXML
private ImageView c8;
@FXML
private ImageView c0;



Node[][] imageViewsTab = {{c1, c2, c3}, {c4, c5, c6}, {c7, c8, c0}};
Node[] imageViews = {c1,c2,c3,c4,c5,c6,c7,c8,c0};
Taquin taquin = new Taquin(imageViewsTab);


public void ouvrirTaquin(MouseEvent mouseEvent){

    gridPane.add(imageViews[0], 0, 0); //(imageView, colonne, ligne)
    gridPane.add(c2, 1, 0);
    gridPane.add(c3, 2, 0);
    gridPane.add(c4, 0, 1);
    gridPane.add(c5, 1, 1);
    gridPane.add(c6,2 , 1);
    gridPane.add(c7, 0, 2);
    gridPane.add(c8, 1, 2);
    gridPane.add(c0, 2, 2);

    if(mouseEvent.getSource() == ouvrirTaquin){
        gridPane.toFront();
    }
}

This code doesn't work but I don't know why TT but it work when I change this line gridPane.add(imageViews[0], 0, 0); -> gridPane.add(c1, 0,0);

Here are the errors :

    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1862)
    at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1729)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3563)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3865)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1851)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2584)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:412)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
    at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
    at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1857)
    ... 29 more
Caused by: java.lang.NullPointerException
    at javafx.graphics/javafx.scene.layout.Pane.setConstraint(Pane.java:111)
    at javafx.graphics/javafx.scene.layout.GridPane.setRowIndex(GridPane.java:299)
    at javafx.graphics/javafx.scene.layout.GridPane.setConstraints(GridPane.java:556)
    at javafx.graphics/javafx.scene.layout.GridPane.add(GridPane.java:973)
    at Stages.Controller.ouvrirTaquin(Controller.java:513)
    ... 40 more

Thank you for your help ^^ (Sorry for my english hehe)

snsdtiti
  • 35
  • 6
  • 1
    Something is null that shouldn't be. What is on line 513 in your Controller.java? – theChoosyBeggar Dec 05 '20 at 17:22
  • At 513 I have the problematic ligne.... gridPane.add(imageViews[0], 0, 0); – snsdtiti Dec 05 '20 at 19:26
  • Perhaps your ImageViews array is getting modified some how. I would honestly just use ```gridpane.add(c1, 0, 0);``` if that is actually working. If you really want to know whats wrong you may have to run the debugger and add a break point in the ```Pane.setConstraint()``` to see why things are null. – theChoosyBeggar Dec 05 '20 at 19:36
  • I can't really use (c1,0,0) because I want to change c1 with condition(when I use some fonction) ^^ thank you for your response – snsdtiti Dec 06 '20 at 10:17

1 Answers1

2

When this line of your code is executed...

Node[] imageViews = {c1,c2,c3,c4,c5,c6,c7,c8,c0};

all the ImageView variables are still null.

You should initialize imageViews in the initialize() method in your controller class.

public void initialize() {
    imageViews = {c1,c2,c3,c4,c5,c6,c7,c8,c0};
}

Refer to this question and answer.

Abra
  • 19,142
  • 7
  • 29
  • 41
  • Thank you ^^ but why are my ImageView are still null ? I already put them in the scene builder and connect them.... I will try the initialize method then ^^ – snsdtiti Dec 06 '20 at 10:20
  • @snsdtiti run your code through a debugger and verify for yourself that they are all null. Note that [Scene Builder](https://gluonhq.com/products/scene-builder/) only generates [java] code. What you see in _Scene Builder_ is not your actual code running. – Abra Dec 06 '20 at 10:25