Object reference not set to an instance of an object in c# class.

I have made 2 c# classes to use in a path finding algorithm by following a tutorial: a cell class and a grid class.
When I access components from the cell class by using the grid class I get the “Object reference not set to an instance of an object error” but I couldn’t figure out why.

This is the first script attached to a random game object to use the grid class

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class agentScript : MonoBehaviour {

	public Transform ground;
	public Transform enemy;
	ground pathFind;
	
	void Start () 
	{
		pathFind = new ground(transform, enemy.transform, ground.transform);
		pathFind.Start();
	}
	
	
	
	
	void Update () 
	{
		pathFind.Update();
	}
}

This is the grid script I use to create the actual script:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;



	public class ground
	{
		
		Transform theAgent;
		Transform theEnemy;
		Transform theGround;
		
		Vector3 topLeftCorner;
	    cellClass theCell;
		cellClass[,] cellArray;
		int a;
		float cellSize;
		int arraySize ;
		int x;
		int z;
		int distance;
		RaycastHit hit;
		Vector3 currentPosition;
		
		public ground(Transform agent, Transform enemy, Transform ground)
		{
			theAgent = agent;
			theEnemy = enemy;
			theGround = ground;
		}
		
		public void Start()
		{
			a = 4;
			arraySize = 9;
			cellSize = theGround.transform.localScale.z / a;///the size of each cell
			cellArray = new cellClass[arraySize,arraySize];////the size of the array
			topLeftCorner = new Vector3(theGround.transform.position.x /2, theGround.transform.position.y, theGround.transform.position.z/2);
			
			for(x = 1; x < cellSize ; x++)
			{
				for(z = 1;z < cellSize ; z++)
				{
					
					cellArray[x,z] = theCell;////fill the x,z array with cells
					cellArray[x,z].cellPos = topLeftCorner + new Vector3(x*cellSize,0,z*cellSize);//THIS IS WHERE I GET THE FIRST ERROR????
					
				}
			}
		}
		
		public void Update()
		{
			for(x = 1; x<cellSize; x++)
			{
				for(z = 1; z < cellSize; z++)
				{
					if(Physics.Raycast (cellArray[x,z].cellPos, Vector3.up, out hit, distance))//THIS IS WHERE I GET THE SECOND ERROR DUE TO THE cellArray
					{
						//print("abba");
					}
					Debug.DrawRay (cellArray[x,z].cellPos,new Vector3(0,distance,0), Color.red);
				}
			}
		
	
	
	
	
	
		}
		
	}

I get the errors because I try to access the cellPos variable in the cellClass by using the cellArray[x,z].cellPos

The last script is the cellClass script
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

    public class cellClass //: System.ValueType
{

	Transform theAgent;
	Transform theEnemy;
	public Vector3 cellPos;
	float distance;
	bool obstacle;
	
	public cellClass(Transform agent, Transform enemy, Vector3 pos )
	{
		theAgent = agent;
		theEnemy = enemy;
		cellPos = pos;
		
		
		
	}
	
	
	
}

I need to know what am I doing wrong and i also need to know if my cellClass can extend System.ValueType.

You need to do some research on the difference between accessing something by reference and by value. Classes are referenced ‘by reference.’ This means that an array of these classes will just be an array of references…all initially null. In order to fill the array, you need new instances of the class for each array reference. Note you a commet that says “//: System.ValueType”, but what you’ve created is not a value type. If this code is translated from Javascript, then you can consider making ‘cellClass’ a ‘struct’ rather than a ‘class’ to solve your problem. Alternately, you could initialize all the positions in the array with instances of ‘cellClass’ created by the ‘new’ operator.

On line 46 you have:

cellArray[x,z] = theCell;

But the ‘theCell’ is a private variable that you never initialize. So you are assigning an uninitialized/null value to the position in the array. This is why you get an error on the next line.