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

I am new to Unity and C# and still learning. I get the following error when i try to instantiate a prefab;

NullReferenceException: Object reference not set to an instance of an object
SpriteScript.OnMouseDrag () (at Assets/Scripts/SpriteScript.cs:49)
UnityEngine.SendMouseEvents:smile:oSendMouseEvents(Int32, Int32)

I am using this code where the error occurs:

using UnityEngine;
using System.Collections;

public class SpriteScript : MonoBehaviour {

	float waitTime = 1.0f;
	float downTime;
	bool isHandled = false;
	float lastClick = 0;
	bool mouseOver = false;
	GameObject theMultiIcon;

	void OnMouseEnter(){
		mouseOver = true;
	}
	
	void OnMouseExit()
	{  
		//clear the object under the mouse when dragging rather than click and hold.
		mouseOver = false;
		isHandled = true;
	}
	
	void OnMouseDown () {
		print ("OnMouseDown");
		//start recording the time when a key is pressed and held.
		downTime = Time.time;
		isHandled = false;
		
		//look for a double click
		if(Time.time-lastClick < 0.3){
			// do something
			Debug.Log("You double clicked the target.");
		}
		
		lastClick = Time.time;
	}
	
	void OnMouseDrag() {
		//open a menu when the key has been held for long enough.
		if((Time.time > downTime + waitTime)  !isHandled  mouseOver){
			isHandled = true;// reset the timer for the next button press
			//add markup menue script here.
			Debug.Log ("MouseKey was pressed and held for over " + waitTime + " secounds.");

			theMultiIcon = GameObject.Find("multi-icon");
			theMultiIcon.transform.position = new Vector3(3.2f, 4.5f, 1f);
			Instantiate(theMultiIcon);
		}
	}


}

I’d suggest you to create a Prefab with the GameObject you want to Instantiate. Make ā€œtheMultiIconā€ public, drag your prefab in and Instantiate that. Try to avoid any use of GameObject.Find since its just a waste of performance

What happens if ā€œmulti-iconā€ game object does not exsists in the scene? :wink:
I think, after this line

theMultiIcon = GameObject.Find("multi-icon");

the theMultiIcon object will be equal null, and after this you will get a NullReferenceException…

Thank you for helping me, I solved it by using Resources.Load :

theMultiIcon = Resources.Load("multi-icon") as GameObject;