Checking array fails each time. But change anything and resave and it works until unity closes......

Ok this has been killing me for hours. And to top it all off it is just an array! I thought the days of them being an issue was behind me but apparently not.

So here is the problem line:

tempInt = selectedDraft.sections[0].ataQuestions.Length;

Selected draft is a class with an array or another class sections. Inside each section is a series of questions (another class).

I just want to check if there are no questions in that section. This gives me the error:

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

I’m ok with it being null. But When I check it in this :

if (selectedDraft.sections [0].ataQuestions.Length != null) {
			foreach(AllThatApply obj in selectedDraft.sections[0].ataQuestions){
				//Doing stuff
			}
		}

It gives me the null exception on the foreach loop. Why would it get to that point with the null check beforehand?

The craziest thing is if I make ANY change (even add a comment) and save… IT WILL WORK. That is until I stop playing the game. Then it will fail until I save something at runtime again. Why the double standard? What am I missing?? I will be driving home after writing this but will respond to questions as soon as I can.

Thank you for you time, I know it was a long read.

do not check if ataQuestions.Length for nullness, check ataQuestions itself! because if its null, you cant access the Length property.

so instead of if (selectedDraft.sections[0].ataQuestions.Length != null)
use if (selectedDraft.sections[0].ataQuestions != null)