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?

5
  • Almost all cases of NullReferenceException are the same. Please see "What is a NullReferenceException in .NET?" for some hints. Commented Feb 28, 2013 at 2: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. Commented Feb 28, 2013 at 2: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 Stack Overflow. Commented Feb 28, 2013 at 2:48
  • Instead of using object initializers try setting the object and then setting each property individually and you might find the actual culprit. Commented Feb 28, 2013 at 2:52
  • Thanks to the both of you -- on Jace's and juharr's suggestions I broke the initializer up and found the real culprit. Commented Feb 28, 2013 at 3:34

2 Answers 2

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
};
Sign up to request clarification or add additional context in comments.

1 Comment

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!
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 () {

3 Comments

The parentheses are not required when doing object initialization expressions.
Another thought after reading @Jace - Make sure your constructor initializes whatever is needed in "//large amount of object initializing here" Constructor runs before initializers!
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.