Q: Why when I add an object to the grid it doesn't work I'm tring to make a Snake game with javafx, neatBeans and scenebuilder. And I don't know why but when I call the method "addBody()" in the Thread that is running the game movement it throws and exception and stops running.
When the method is called by the constructor it works, but when is called by the thread it doest
The line that makes the exception is the "GridBoar.add(...)"
This is the part of the code
    private void addBody(){
        Circle Body = new Circle();  
        Body.setRadius(20); //Radio 20
        Body.setFill(javafx.scene.paint.Color.GREEN); //Color del cuerpo verde

        int[] lastPos = SnakeHead.get(SnakeHead.size()-1).getPosition();
        GridBoar.add(Body, lastPos[0], lastPos[1]); //THIS LINE 
        Snake s = new Snake(lastPos, GridBoar, Body, "1");
        SnakeHead.add(s);
    }

This is the full Code (there is also the Snake code and the controller but I didn't atached them) The method is called at the end of the Code by "moveHead()" method
package javafxmlapplication;


import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import java.util.List;
import java.util.ArrayList;
import javafx.scene.shape.Circle;
/**
 *
 * @author miquel
 */
public class SnakeStart extends Thread{
    
    List<Snake> SnakeHead;
    int[] ApplePos = new int[2];
    Circle Apple;
    Text TextMovement;
    GridPane GridBoar;
    Text Score;
   
    
    public SnakeStart(GridPane p, Text score, Text tm){
        SnakeHead = new ArrayList<Snake>();
       
        TextMovement = tm;
        Score = score;
        GridBoar = p;
        //------------------------------
        createHead();
        //-------------------------------
        addBody();        
        //-------------------------------
        createApple();
    }
    private void createHead(){
        Circle Node = new Circle();  
        Node.setRadius(20); //Radio 20
        Node.setFill(javafx.scene.paint.Color.BLUE); //Color de la cabeza Azul

        GridBoar.add(Node, (int) GridBoar.getColumnCount()/2, (int) GridBoar.getRowCount()/2);
        Snake s = new Snake(new int[]{(int) GridBoar.getColumnCount()/2, (int) GridBoar.getRowCount()/2},
                GridBoar, Node, "1");
        SnakeHead.add(s);
    }
    private void createApple(){
        Apple = new Circle();  
        Apple.setRadius(20); //Radio 20
        Apple.setFill(javafx.scene.paint.Color.RED); //Color de la manzana rojo
        GridBoar.add(Apple, 0, 0);
        newApple();
    }
    
    @Override
    public void run() {
        while(true){
            move();
            try{Thread.sleep(250);} catch(InterruptedException e){}
        }
    }
    
    private void addBody(){
        Circle Body = new Circle();  
        Body.setRadius(20); //Radio 20
        Body.setFill(javafx.scene.paint.Color.GREEN); //Color del cuerpo verde

        int[] lastPos = SnakeHead.get(SnakeHead.size()-1).getPosition();
        GridBoar.add(Body, lastPos[0], lastPos[1]);
        Snake s = new Snake(lastPos, GridBoar, Body, "1");
        SnakeHead.add(s);
    }
    
    private void newApple(){       
        int xTam = GridBoar.getColumnCount();
        int yTam = GridBoar.getRowCount();
        do{
            int xPos = (int) (Math.random() * xTam);
            int yPos = (int) (Math.random() * yTam);
            ApplePos[0] = xPos;
            ApplePos[1] = yPos;
        }while(isOcuped(ApplePos));
        
        GridBoar.setConstraints(Apple, ApplePos[0], ApplePos[1]);
    }
    private boolean isOcuped(int[] pos){
        boolean res = false;
        int[] auxPos;
        for(int i = 0; i < SnakeHead.size() && !res; i++){
            auxPos = SnakeHead.get(i).getPosition();
            if(auxPos[0] == pos[0] && auxPos[1] == auxPos[1]) res = true;
        }
        return res;
    }
    
    
    private void addPoint(){
        int points = Integer.parseInt(Score.getText());
        points++;
        Score.setText(SnakeHead.size() + "");
    }
    
    private void move(){
        //mover cabeza
        int[] auxPos = SnakeHead.get(0).getPosition(); //Posición de la cabeza
        moveHead();
        //-----------------------------
        
        
        //-----------------------------
        //mover cuerpo
        for(int i = 1; i < SnakeHead.size(); i++){
            auxPos = SnakeHead.get(i).move(auxPos);
        }
    }
    private void moveHead(){
        int[] pos = SnakeHead.get(0).getPosition();
        String Direction = TextMovement.getText();
        switch(Direction){
            case "UP":
                    //rowCont = (GridBoar.getRowIndex(body) - 1) % GridBoar.getRowCount();
                    //if(rowCont < 0) rowCont = GridBoar.getRowCount()-1;
                    //GridBoar.setRowIndex(body,rowCont);
                    
            pos[1] = (pos[1] - 1) % GridBoar.getRowCount();
            if(pos[1] < 0) pos[1] = GridBoar.getRowCount() - 1;
            SnakeHead.get(0).move(pos);
            break;
            case "DOWN":
                    //rowCont = (GridBoar.getRowIndex(body) + 1) % GridBoar.getRowCount();
                    //GridBoar.setRowIndex(body,rowCont);
                    
            pos[1] = (pos[1] + 1) % GridBoar.getRowCount();
            SnakeHead.get(0).move(pos);
            break;
            case "LEFT":
                    //colCont = (GridBoar.getColumnIndex(body) - 1) % GridBoar.getColumnCount();
                    //if(colCont < 0) colCont = GridBoar.getColumnCount()-1;
                    //GridBoar.setColumnIndex(body,colCont);
                    
            pos[0] = (pos[0] - 1) % GridBoar.getColumnCount();
            if(pos[0] < 0) pos[0] = GridBoar.getColumnCount() - 1;
            SnakeHead.get(0).move(pos);
            break;
            case "RIGHT":
                    //colCont = (GridBoar.getColumnIndex(body) + 1) % GridBoar.getColumnCount();
                    //GridBoar.setColumnIndex(body,colCont);
                    
            pos[0] = (pos[0] + 1) % GridBoar.getColumnCount();
            SnakeHead.get(0).move(pos);
            break;
            default:
                    //colCont = (GridBoar.getColumnIndex(body) + 1) % GridBoar.getColumnCount();
                    //GridBoar.setColumnIndex(body,colCont);
                    
            pos[0] = (pos[0] + 1) % GridBoar.getColumnCount();
            SnakeHead.get(0).move(pos);       
            }
        int[] headPos = SnakeHead.get(0).getPosition();
        if(headPos[0] == ApplePos[0] && headPos[1] == ApplePos[1]){
            newApple();
            addPoint();
            addBody(); // HERE IS IT CALLED 
        }      
    }
    
    
}


