Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Null Reference Exception when trying to reach an element of an array

Discussion in 'Scripting' started by Bober2496, Aug 14, 2020.

  1. Bober2496

    Bober2496

    Joined:
    Jun 17, 2020
    Posts:
    1
    First I made an array in my MapGrid<TGridArray> class.
    protected TGridArray[,] gridArray;


    I have another class called PathNode
    Code (CSharp):
    1. public class PathNode
    2.     {
    3.         public Vector2Int coordinate;
    4.         public int gCost, hCost, fCost;
    5.         public PathNode parent;
    6.         public int weightFactor;
    7.  
    8.         public PathNode()
    9.         {
    10.  
    11.         }
    12.     }
    And I have a subclass which is inherit the MapGrid class as well as the gridArray variable array.
    Code (CSharp):
    1. public class NavigationGrid : MapGrid<PathNode>
    2.     {
    3.         //Constructor
    4.         public NavigationGrid(int width, int height, float cellSize, Vector3 startingPosition = default)
    5.         {
    6.             this.width = width;
    7.             this.height = height;
    8.             this.cellSize = cellSize;
    9.             this.startingPosition = startingPosition;
    10.  
    11.             gridArray = new PathNode[width, height];
    12.             for (int x = 0; x < gridArray.GetLength(0); x++)
    13.                 for (int y = 0; y < gridArray.GetLength(1); y++)
    14.                     gridArray[x, y].coordinate = new Vector2Int(x, y);
    15.         }
    And when I want to make a NavigationGrid in my monobehaviour script I get an error because of this piece of the constructor:
    gridArray[x, y].coordinate = new Vector2Int(x, y);


    I want to use in the monobehaviour script like this:
    Code (CSharp):
    1. grid = new NavigationGrid(width, height, cellSize, originalPosition);
    The error message says this:

    NullReferenceException: Object reference not set to an instance of an object
    Bober.UnityTools.NavigationGrid..ctor (System.Int32 width, System.Int32 height, System.Single cellSize, UnityEngine.Vector3 startingPosition) (at Assets/Scripts/MapGrid.cs:202)
    Testing.Start () (at Assets/Scripts/Testing.cs:34)


    I made some debug and the
    gridArray = new PathNode[width, height];

    line doing its job well and give the correct size to the array but after that i cannot write to the [0, 0] element of the array.
    What is the problem with it?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,777
    You are creating your array fine but it's an array of nulls. So, on that line (line 14 in the post, presumably line 202 in the actual file), you are trying to set the field "coordinate" of a null object. You need a line like this there first:
    Code (csharp):
    1. gridArray[x, y] = new PathNode();
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,314
    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.