Null ignorance

I have a piece of code that in the if statement(line 5 below) checks if an GameObject is not null.

Now in the following if statement(line 7 below) it finds a null object reference,

which to me should logically not happen. since it found an object earlier.

Here is the code:

for(int OB = 1; OB < 9; OB = OB +1)
{
    GameObject cardSpot = GameObject.Find("EnemySlot"+OB);
	EnemyCardSpot Espot = cardSpot.GetComponent<EnemyCardSpot>();
	if(Espot.thatCard != null)
	{
		if(Espot.enemyCard.me.Energy < Espot.enemyCard.me.DataEnergyMax)
        {
			if(Espot.enemyCard.me.Energy < 0 && Espot.enemyCard.me.Type != "Infantry")
			{
				Espot.enemyCard.me.Energy = Espot.enemyCard.me.Energy +1;
			}
			else
			{
				Espot.enemyCard.me.Energy = Espot.enemyCard.me.DataEnergyMax;
				Espot.enemyCard.Active = true;
			}
		}
		else
	    {
			Espot.enemyCard.Active = true;
		}
	}
}

In your script above at line 5 it finds the Component Espot which is available and then it moves to execute the code inside the if statement for line 5. But on line 7 it is not able to find either ‘enemyCard’ or ‘me’ inside your Espot component object and that is why there is null reference error.

Check if you have enemyCard and me are not null before accessing it and you can find the cause for the error.

Just put

Debug.Log(Espot.enemyCard);
Debug.Log(Espot.enemyCard.me);

once you enter the if braces of yours after line 5 and you will come to know the null object.