1

I have a GameObject we'll call the GM. Attached to it is a script that's meant to be the primary logical controller for a game.

In that script, somewhere, I have:

private dbEquipment equipment_database = new dbEquipment();

The relevant snippet from dbEquipment.cs:

public class dbEquipment {
    private int total_items = 13;
    private clEquipment[] _master_equipment_list;

    public dbEquipment() {
        _master_equipment_list = new clEquipment[total_items];
        _master_equipment_list[0] = new clEquipment {
            ... //large amount of object initializing here
        };
        ... //etc, for all 13 items
    }
}

When I run Unity, I get:

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

Pointed at the line:

_master_equipment_list[0] = new clEquipment { ...

I tried running through the array and initializing every clEquipment object to an empty clEquipment() first:

for(int x = 0; x < total_items; x++) { _master_equipment_list[x] = new clEquipment(); }

just to be totally sure that the array was actually filled, but I got the same result.

I've also tried changing it to be a List<clEquipment>, and changing everything appropriately -- no dice.

Any ideas?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user462879
  • 187
  • 1
  • 13
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Feb 28 '13 at 02:43
  • Thanks for the input! I know what a `NullReferenceException` is. In this case, I'm just stumped because this is one of those "too simple to fail" cases, so I thought that maybe there was some Unity-specific quirk that someone might know about. – user462879 Feb 28 '13 at 02:46
  • 1
    In that case, you should also know how to debug it and find out what's null. You'll probably find the answer by debugging before anyone gives you the answer on [so]. – John Saunders Feb 28 '13 at 02:48
  • Instead of using object initializers try setting the object and then setting each property individually and you might find the actual culprit. – juharr Feb 28 '13 at 02:52
  • Thanks to the both of you -- on Jace's and juharr's suggestions I broke the initializer up and found the real culprit. – user462879 Feb 28 '13 at 03:34

2 Answers2

4

My guess is that you may been including a null reference in the section that says //large amount of object initializing here when you create a new clEquipment.

_master_equipment_list[0] = new clEquipment {
    ... //check for nulls here
};
Jace
  • 3,052
  • 2
  • 22
  • 33
  • I misunderstood how object initializers were treated -- the culprit was indeed in there; before now, I thought that it would point the error at that specific line, not realizing that no matter how pretty the whole initializer is in my IDE, it actually is, in fact, one line. Thanks! – user462879 Feb 28 '13 at 03:32
0

You might want to post the code for the clEquipment class. You say you tried initializing every object...did you do that before the break line? If it didn't break, thats a good sign.

Also, hard to tell from your code, but do you need a "()" in the initialization where it breaks? Just a thought

_master_equipment_list[0] = new clEquipment () {
7200rpm
  • 548
  • 1
  • 4
  • 14
  • 1
    The parentheses are not required when doing object initialization expressions. – juharr Feb 28 '13 at 02:51
  • Another thought after reading @Jace - Make sure your constructor initializes whatever is needed in "//large amount of object initializing here" Constructor runs before initializers! – 7200rpm Feb 28 '13 at 02:51
  • The constructor shouldn't need to initialize anything for the object initialization to work. That's the point of it. It's more likely that the null value is something he's using to set one of the properties with. – juharr Feb 28 '13 at 02:54