NullReferenceException: Except it is referenced

The script that is posted below is meant to spawn copies of the object it is attached to in all 4 directions on the board it is on, if there’s free space. The board itself is 6*10 square grid, with boundaries around it, etc.
And it USUALLY successfully clones itself and when it hits an obstacle it stops cloning And as soon as it does, I get a nullreferenceexception error. And it stops cloning itself altogether.

using UnityEngine;
using System.Collections;

public class PathfinderAgent : MonoBehaviour {

	public static int moves;
	private Vector3 cRight, cLeft, cUp, cDown, test;
	private Vector3 gRight, gLeft, gUp, gDown;
	private DetectSelection detectSelection; 

	bool CheckDir(Vector3 direction){
		if (detectSelection.Raycast (direction).collider.gameObject.tag == "Background") {
			return true;
		} else {
			return false;
		}
	}

	void Start () {
		detectSelection = GameObject.FindGameObjectWithTag ("MainCamera").GetComponent<DetectSelection>();

		cRight = new Vector3 (gameObject.transform.position.x + 1.5f, gameObject.transform.position.y + 0.5f, gameObject.transform.position.z);
		cLeft = new Vector3 (gameObject.transform.position.x - 0.5f, gameObject.transform.position.y + 0.5f, gameObject.transform.position.z);
		cUp = new Vector3 (gameObject.transform.position.x + 0.5f, gameObject.transform.position.y + 1.5f, gameObject.transform.position.z);
		cDown = new Vector3 (gameObject.transform.position.x + 0.5f, gameObject.transform.position.y - 0.5f, gameObject.transform.position.z);

		gRight = new Vector3 (gameObject.transform.position.x + 1.0f, gameObject.transform.position.y, gameObject.transform.position.z);
		gLeft = new Vector3 (gameObject.transform.position.x - 1.0f, gameObject.transform.position.y, gameObject.transform.position.z);
		gUp = new Vector3 (gameObject.transform.position.x, gameObject.transform.position.y + 1.0f, gameObject.transform.position.z);
		gDown = new Vector3 (gameObject.transform.position.x, gameObject.transform.position.y - 1.0f, gameObject.transform.position.z);


		//Right
		if (CheckDir(cRight))	{
			Instantiate(gameObject, gRight, Quaternion.identity);
		}

		//Left
		if (CheckDir(cLeft))	{
			Instantiate(gameObject, gLeft, Quaternion.identity);
		}

		//Up
		if (CheckDir(cUp))	{
			Instantiate(gameObject, gUp, Quaternion.identity);
		}

		//Down
		if (CheckDir(cDown))	{
			Instantiate(gameObject, gDown, Quaternion.identity);
		}
	}
}//end of script

The error points to line 12 and the if statement that calls it. What am I doing wrong?

So I’m an idiot. And here I thought the else branch would cover for cases when raycast returns null. Which would be at the edges of the board. Solved it by adding a boundary around the board…