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

Okay, this is my first attempt at Robot Repair… I’ve got errors on lines 41,59 91. Below is my script. If anyone can help me get this resolved I will be extremely thankful!

var cols:int = 4; 
var rows: int = 4;
var totalCards:int = cols*rows; //16 cards total
var matchesNeededToWin:int = totalCards * 0.5; //if there are 16 cards you need 8 to win and clear the board
var matchesMade:int = 0; //at the start there have no been any matches made
var cardW:int = 100; // 100pixels wide
var cardH:int = 100; //100 pixels tall
var aCards:Array; //we'll store the cards we collect here
var aGrid:Array; //keeps track of shuffeled and delt cards
var aCardsFlipped:ArrayList; //stores cards the player has flipped
var playerCanClick:boolean; //we'll use this to prevent the player fromclicking buttons when we dont want him to.
var playerHasWon:boolean = false; //stores whether or not a player has won should start at false.



function Start () 
{
playerCanClick = true;  // we should let the player press play
BuildDeck(); 
aCards = new Array ();  //initialize the array as an empty list
aGrid = new Array ();
aCardsFlipped = new ArrayList();
for (var i=0; i<rows; i++)
	{
		aGrid[i] = new Array(); //creates a new empty array at index i
		for (var j: int=0; j<cols; j++)
		{
		var someNum:int = Random.Range(0, aCards.length);
		aGrid[i][j] = aCards[someNum];
		aCards.RemoveAt(someNum);
	}
  }
}




function OnGUI () 
{      //possible error on my end..change function back to update?
	GUILayout.BeginArea(Rect(0,0,Screen.width, Screen.height));
	BuildGrid();
	GUILayout.EndArea();
	print("Building Grid!");

}



function BuildGrid()
{
	GUILayout.BeginVertical();
	GUILayout.FlexibleSpace();
	for (var i=0; i<rows; i++)
	{
	GUILayout.BeginHorizontal();
	GUILayout.FlexibleSpace();
	for (var j=0; j<cols; j++)
	{
	var card:Object = aGrid[i][j];
	if (GUILayout.Button(Resources.Load(card.img),GUILayout.Width(cardW)))
	{
	Debug.Log(card.img);
}
}
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
}
GUILayout.FlexibleSpace();
GUILayout.EndVertical();
}



	function BuildDeck () {
	
	var totalRobots : int = 4;  // We have 4 robots
	var card : Object;  // stores a reference to a card
	var id : int = 0;
	
	for (var i=0; i<totalRobots; i++)
		{
		   var aRobotParts:Array = ["Head", "Arm", "Leg"];
		   for (var j=0; j<2; j++)
		{
			var someNum:int = Random.Range(0, aRobotParts.length);
			var theMissingPart:String = aRobotParts[someNum];
			
			aRobotParts.RemoveAt(someNum);
			
			card = new Card("robot" + (i+1) + "Missing" + theMissingPart, id);
			aCards.Add(card);
			
			card = new Card("robot" + (i+1) + theMissingPart, id);
			aCards.Add(card);
			id++;
			
	    }
	}	
}



class Card extends System.Object
{
var isFaceUp : boolean = false;
var isMatched : boolean = false;
var img : String;
var id : int;

function Card (img:String, id:int) 
{
this.img = img;
this.id = id;
}
}

When posting code, please use code tags: http://forum.unity3d.com/threads/143875-Using-code-tags-properly.

Look…nobody is going to fix your code when you ask like this. You are basically dumping your script you copied from somewhere on the web and asking why it isn’t working. Heck you have 3 different errors (and we have no idea what lines those refer to since you didn’t include line numbers). If you had a specific problem…but you just have errors because you don’t know what you are doing. Why don’t you slow down, and read about what you are doing. Have you googled the error message? Have you spent a couple hours learning about whatever it is that isn’t working? don’t expect us to do your work for you unless you are paying…

I didn’t copy the code from the web. I am working out of the book Unity 3D Game Development by Example from Ryan Henson Creighton. I’ve spent quite a bit of time looking online but haven’t been able to find an answer, which is why I thought asking in the community would be helpful (maybe not the right thing to assume). I would never ask for help without having first made a significant effort on my side. I appreciate the replies, even though I am being ripped apart here, which was kind of expected because I know I am new to this whole thing. I will continue to try and fix it on my own. Thanks anyways.

@G420LAW - you’ll slowly learn that the community here is amazing, and many many of us are patient and helpful. All these dudes expect is that you spend 1 minute learning how to format your code, and another 1 minute taking the time to tell us what the errors are on the lines you mention. Saying “I’ve spent quite a bit of time looking online” is of course good, but highlights that you haven’t spent that time getting your question improved. And believe me, if you think you are being ripped apart, you’ve not met the more hostile members of the community.

Actually, now I’ve helped you and taken 30 seconds to put code tags around your code, you can start to see the problem the community has. You have an error on line 41, but line 41 is just a function call, so it’s kinda hard to guess what the problem is.

@Graham Dunnett - Thank you for the explanation! I am learning how the community and Unity works in general. I do appreciate any feedback whether its positive or not. Some hazing for the freshmen is expected. I will keep looking for a fix to the issues I have on here, as well as further my knowledge on JS in general. When I am up to speed I will get back to this and be more grateful for the responses thus far.

The errors read as such:
NullReferenceException: Object reference not set to an instance of an object
NewGameScript.BuildGrid () (at Assets/Scripts/NewGameScript.js:59)
NewGameScript.OnGUI () (at Assets/Scripts/NewGameScript.js:41)
NewGameScript.BuildDeck () (at Assets/Scripts/NewGameScript.js:91)
NewGameScript.Start () (at Assets/Scripts/NewGameScript.js:19)