Error when calling a var that was just defined

I am using a script to make an Animated Sprite class (found here: [https://bit.ly/3cIZX68]) and it is giving me the error saying that my AnimatedSprite anim was null, even though it was defined directly before. Here is the snippet of code.

		'if (isMoving)
		{
			anim = animations[1];
			anim.Update(gameTime);
		}

		else
		{
			anim = animations[0];
			anim.Update(gameTime); //error is on this line saying anim is null
		}`

Thanks for your help.

animations[0] could hold a null value.

It is defined with:

public static void Load(ContentManager content)
	{
		playerIcon = content.Load<Texture2D>("Player/playerIcon");
		playerIdleSprite = content.Load<Texture2D>("Player/playerIdle");
		playerRunSprite = content.Load<Texture2D>("Player/playerRun");

		player.animations[0] = new AnimatedSprite(playerIdleSprite, 1, 6, 0.15d);
		player.animations[1] = new AnimatedSprite(playerRunSprite, 1, 4, 0.15d);
	}

The code you’re posting is too incomplete for me (or others) to get an accurate picture of what’s going on. From what you’ve posted, I can only offer the following…

In your first post, you’re referencing the animations array directly. In your second post, you’re showing that player.animations is initialized to a value. I can make a lot of assumptions about how you’re writing your code, but sticking just to what’s shown, there’s no evidence that “animations” and “player.animations” are referring to the same array.

It might be a good idea to throw a few breakpoints in your initializations, and where the actual data is used. Are they the same object?

I solved the problem anyway