Why am I getting NullReferenceException: Object Reference...

void ScanTerrain(){

		RaycastHit detectHeight;

		stepHeight = stepHeight + 1; //Ensure ray starts below step
		wallHeight = wallHeight + 1; //Ensure ray starts below wall

		Vector3 rayOrigin = new Vector3(0,15f,0); 
		Vector3 centrePoint = new Vector3 (0, 0, 0); 

		bool checkIfSecondHit = false; 


		for (int x = 1; x < (terrainX*2); x = x + moveUnit) {
			rayOrigin.x = x; 
			for(int z = 1; z < (terrainZ*2); z = z + moveUnit){
				rayOrigin.z = -z; 

				//Send first ray and find centre point of tile
				if (Physics.Raycast (rayOrigin, rayDirection, out detectHeight)){
					centrePoint.x = rayOrigin.x; 
					centrePoint.z = rayOrigin.z; 
					centrePoint.y = rayOrigin.y - detectHeight.distance; 
					ComputeMesh (centrePoint); 

					Debug.Log (detectHeight.transform.tag); 
					Debug.Log (rayOrigin); 
					//Compute second ray depending on height info of features by setting origin of second ray
					if (detectHeight.transform.tag == "Ruins"){
						rayOrigin.y = rayOrigin.y - detectHeight.distance; 
						if (Physics.Raycast(rayOrigin, rayDirection, out detectHeight)){
							centrePoint.x = rayOrigin.x; 
							centrePoint.z = rayOrigin.z; 
							centrePoint.y = rayOrigin.y - detectHeight.distance; 
							ComputeMesh (centrePoint); 
							checkIfSecondHit = true;
						}
					}
					if (detectHeight.transform.tag == "Wall" && !checkIfSecondHit){
						rayOrigin.y = rayOrigin.y - (detectHeight.distance + wallHeight); 
						if (Physics.Raycast(rayOrigin, rayDirection, out detectHeight)){
							centrePoint.x = rayOrigin.x; 
							centrePoint.z = rayOrigin.z; 
							centrePoint.y = rayOrigin.y - detectHeight.distance; 
							ComputeMesh (centrePoint); 
							checkIfSecondHit = true; 
						}
					} 

					if (detectHeight.transform.tag == "StoneStep" && !checkIfSecondHit){
						rayOrigin.y = rayOrigin.y - (detectHeight.distance + stepHeight); 
						if (Physics.Raycast(rayOrigin, rayDirection, out detectHeight)){
							centrePoint.x = rayOrigin.x; 
							centrePoint.z = rayOrigin.z; 
							centrePoint.y = rayOrigin.y - detectHeight.distance; 
							ComputeMesh (centrePoint); 
							checkIfSecondHit = true; 
						}
					}




					//Reset initial Ray height value
					rayOrigin.y = 15f; 
					checkIfSecondHit = false; 

				}

			}

		}

	}


	void ComputeMesh(Vector3 tileCentre){


		//Initialize variables
		Vector3 vertRay = new Vector3 (0,0,0); 
		RaycastHit vertexHeight; 
		vertRay.y = tileCentre.y; 
		Vector3[] vertices = new Vector3[4]; 
		int cornerCount = 0; 

		Dictionary<Vector3, int> tileLookUp = new Dictionary<Vector3, int> (); 
		List<TileMapObject> tileList = new List<TileMapObject> (); 


		//So 4 vertex points means 4 coordinates. Since I have the centre coordinate from tileCentre I can compute the vertex from it
		for (int vx = -1; vx < 1; vx = vx + moveUnit) {
			for (int vz = -1; vz < 1; vz = vz + moveUnit){
			
				vertRay.x = tileCentre.x - vx; 
				vertRay.z = tileCentre.z - vz; 

				if (Physics.Raycast(vertRay, rayDirection, out vertexHeight)){

					vertices[cornerCount].x = vertRay.x; 
					vertices[cornerCount].z = vertRay.z; 

					vertices[cornerCount].y = vertRay.y - vertexHeight.distance; 

					tileList.Add(new TileMapObject(vertices)); 
					tileLookUp.Add(tileCentre, tileList.Count); 

					//cornerCount is to keep track of the vertices for the triangle 
					cornerCount++; 




				}
			}
		}

I get the error on the if statement lines in ScanTerrain()

NullReferenceException: Object reference not set to an instance of an object

LayGrid.ScanTerrain () (at Assets/Scripts/LayGrid.cs:60)

LayGrid.Start () (at Assets/Scripts/LayGrid.cs:17)

Which is at: if (detectHeight.transform.tag == “Wall” && !checkIfSecondHit)

When I check the console debug the coordinates at which it fails is over an object tagged Ruins which has nothing underneath it so it should return false for that if statement, why would it give me this error if the if statement is false at that point?

Thanks

EDIT:: Forgot to mention that this loop runs successfully many times before running into an error. The loop gets stuck at a seemingly arbitrary coordinate, when I changed the order of if statements the coordinate at which it gets stuck changes. However in both cases the tag at which it fails is not the one being referred to by the error.

The GameObject detectHeight does not exist. Remember that when using GameObject.Find(), it is case-sensitive, so this may be why it returns null.