Getting a strange null reference error

Godot Version

4.3.stable.mono

Question

I am trying to make a UI to display resources needed to construct a building. I have a UI script that gets an array of InventoryItems from the building being constructed. InventoryItem is a array, with each element consisting of an ItemDataResource and an amount – the ItemDataResource contains the ItemName, ButtonIconName, etc. for the item

I request the array from the building object, and it comes back OK. I can GD.Print the length of the returned array and GD.Print the ButtonIconName on element[0] of the array, and it works fine, with no error

The problem comes when I try to iterate through the array in a loop. I get a null reference error, even though the GD.Print statement still works

public void UpdateConstructionUI(BuildingConstruction bldg)
    {
        InventoryItem[] itemList = bldg.GetConstructionList();
        
        GD.Print("ConstructionUI: itemList.Length = " + itemList.Length);
        GD.Print("ConstructionUI: " + itemList[0].itemResource.ButtonIconName);
        
        for (int i = 0; i < itemList.Length; i++)
        {
            GD.Print($"ConstructionUI: i = {i}, ButtonIconName = {itemList[i].itemResource.ButtonIconName}");
        
        }

    }

This produces the output:
ConstructionUI: itemList.Length = 2
ConstructionUI: Block.png
i = 0, ButtonIconName = Block.png

Even though the code works and I get the desired output, I get a null reference on the last line and the program dies without completing the loop

Why can I do this using 0 for the index, but when I use i (with a value of 0) it fails?

What am I missing here?

Does every item from the list have an itemResource?

Yes.

But even if one later in the list didn’t, shouldn’t the first element still print?

I can print element [0], but I can’t print element [i] when i=0

That’s the part that’s got me scratching my head

But where does:
i = 0, ButtonIconName = Block.png
come from then?

Sorry, that’s the issue. It prints fine, but then gives a null reference and stops the loop on that same line.

So it executes the line, then dies

Then the second item in your list is null.
You might check your GetConstructionList(); code.

edit OK, after messing around with this for a while, it turns out my array has 2 elements, but the 1st element contains the 2nd element’s data and the 2nd element is null after all…

So now, why?

Is there a trick to passing arrays back from a function in Godot that may be getting me?

edit again

So no matter how many elements are in the array that gets passed it looks like only the last element is actually getting put into the receiving array (as the first element)

OK, I’m an idiot

Over in my other function, I am putting items in the array using a counter - but I never incremented the counter, so everything was going in as the first element

I fixed that and it works now… sorry to have wasted your time trizZzle - thanks for the help!!

1 Like

Hey, no problem! Glad it works now! I forgot to increment a counter recently too. :sweat_smile:

These things happen all the time. Don’t worry about it. :rofl: