Weird null reference exception

Hi all!

I am currently working on a project involving some heavy AI including pathfinding. To check wether i need to move i first check if i have path, then i check if my current step is smaller then the length of the path. Somewhere along the line i get a weird null reference exception. Some code:

private Path _path;
private int _currentStep =  0;

void Update()
{
  if (_path == null)
    return;
  if (_currentStep >= _path.vectorPath.Length)                 //this line gives null reference.
  {
    Debug.Log("Path complete");
    return;
  }
  etc....
}

How come it does not do the return if the path is null. What else might be null in this scenario, the vectorPath? VectorPath is just an array of Vector3, could that be the source of my headaches?

It very likely is that array “vectorPath”, indeed. Either check this as well or ensure that it never is null (e.g. by initializing it to an empty array).