Null an array slot

I keep getting errors but it seems to work correctly. I’ve made an array of one of my classes but ran into a problem. I want to clear all the elements in that slot to be blank / default. Which seems to work but I get this error.

NullReferenceException: Object reference not set to an instance of an object
Inventory.OnGUI () (at Assets/Scripts/Inventory.js:40)

This is my code for displaying the items in the array.

	for(var y = 0; y < EquipMenu.length; y++) {
	
		if(GUI.Button(Rect(Screen.width/2 - 75, Screen.height/2 + ( 20 * y), 100, 20), "" + EquipMenu[y].Name)) {
		
			
			MainInventory.Add(EquipMenu[y]);
			EquipMenu[y] = null;
			
					
		}
	}

You’re getting a null reference exception because you are still trying to access the name (EquipMenu[y].Name) of the object in the EquipMenu array even after you’ve made the element null. You can prevent this by checking if EquipMenu[y] is null or not before you execute your code - i.e.

for(var y = 0; y < EquipMenu.length; y++) {
     
	if(EquipMenu[y] != null) //this will prevent null reference exceptions
	{
       if(GUI.Button(Rect(Screen.width/2 - 75, Screen.height/2 + ( 20 * y), 100, 20), "" + EquipMenu[y].Name)) 
		{
  
         MainInventory.Add(EquipMenu[y]);
         EquipMenu[y] = null;
  
       }
	}
}