#Help with a NullReferenceException

1 messages · Page 1 of 1 (latest)

tame mason
#

I need help figuring out the problem here:

public class Colony : MonoBehaviour
{
  //...  
  public Dictionary<ResourceType, float> ResourceAmounts = new Dictionary<ResourceType, float>();
  
  void Awake()
    {
        Debug.Log("New Colony starts up...");
        _transform = transform;

        InitializeColony();
        //...
    }

  // Update is called once per frame
    void Start()
    {
        MainGUIPanel.Instance.GiveColony(this);
    }

  private void InitializeColony()
    {
        ResourceAmounts[ResourceType.Food] = 100f;
        ResourceAmounts[ResourceType.Water] = 100f;
        ResourceAmounts[ResourceType.Eggs] = 3f;
        Memory = new Memory(this);        
    }
 }

public class MainGUIPanel : MonoBehaviour
{
  public static MainGUIPanel Instance;
  [SerializeField] private Colony _colony;
  //...

  void Start()
    {
        Instance = this;
        // ...
    }

  public void GiveColony(Colony colony)
    {
        _colony = colony;
    }

  public void Update()
    {
        UpdateColonyStats();
        // ...
    }

  private void UpdateColonyStats()
    {
        Colony colony = _colony;

        Dictionary<ResourceType, float> resourceAmounts = colony.ResourceAmounts;
    }

That last line is the one giving me a NullReferenceException and I can't figure out why.

sweet gyro
#

the only possibility is that colony is null

#

if you haven't called GiveColony yet, then it could have no colony

tame mason
#

But I call GiveColony in Start

#

So Colony.Start should finish executing before MainGUIPanel.Update gets called?

sweet gyro
#

If they're both in the scene from the start, this is fine

tame mason
#

ah

#

well they are not

#

MainGUIPanel is attached to an existing canvas. Colony is instantiated by the GameManager

#

essentially

sweet gyro
#

then there's your problem

#

Colony will run Start the frame after it is created

#

but the GUI ran Start on frame 0 and is already updating

tame mason
#

It's currently fine. I get this NullReferenceException a couple hundred times and then it stops. Which makes sense.

#

I just would rather my code not be throwing unhandled exceptions.

sweet gyro
#

your GUI should gracefully handle not having a colony selected, I'd think

tame mason
tame mason
sweet gyro
#

Where?

tame mason
#

I removed it

#

To figure out exactly which part was null. You can see I'm actually accessing fields of _colony and I wasn't sure where the problem really was.

#

ohhhhh

#

I know why

#

lol

#

actually no i don't

#

I had this conditional: if (_colony != null & _colony.ResourceAmounts != null)

#

well fuck me. I added it back and now the exception is gone. Whatever.

#

🤌 got it nice and NullReferenceException-free. Thanks!!!

sweet gyro
#

& doesn't short-circuit, so it will always evaluate both sides

sweet gyro
#

so I would expect an NRE if _colony was null

tame mason
#

I had been assuming it short circuits!

#

strange. every other language i've used has short circuiting

sweet gyro
#

&& and || are the binary conditional logical operators

#

& and | are also used for bitwise operations on numbers

tame mason
#

oh i should be using && and || then?