I’m working with the Google Admin SDK in C# and trying to update user organization information. However, I keep running into a System.NullReferenceException error.
The error happens when I try to assign values to the array elements. What am I doing wrong here? I’m new to working with arrays in C# and could use some guidance.
EDIT: Found the solution! I needed to create new instances for each array element before setting their properties.
Nice job solving it yourself! This trips up tons of developers working with reference types in C#. When you write CompanyInfo[] orgArray = new CompanyInfo[5], you’re just creating an array with 5 empty slots - each one starts as null. You’ve got to create each CompanyInfo object first before setting properties. Fix it like this: orgArray[0] = new CompanyInfo(); orgArray[0].Division = "marketing"; and repeat for each element. I’ve hit this same bug so many times with object arrays, especially coming from other languages that handle object creation differently. Once you get the difference between value types and reference types, it clicks.
Classic gotcha that gets everyone at least once. Array declaration just reserves memory slots - it doesn’t create the objects inside them. Each element stays null until you explicitly create it. I spent hours debugging this exact thing when I started with C#. What clicked for me was thinking of arrays like a parking lot - you’ve got the spaces but no cars parked yet. The Google Admin SDK docs could be way clearer about this. Pro tip: if you know the values upfront, use array initialization syntax and it’ll create the objects automatically.
Same thing happened to me with the admin SDK. new CompanyInfo[5] just creates empty slots - you still need to do new CompanyInfo() for each slot or they’re all null. Google’s docs are pretty terrible at explaining this to beginners, so glad you got it sorted.