null refference exception. right after i dimention the object array??

Hi

The code looks good. Its pretty, its symetrical. It’s suppose to work. but it doesn’t.

I get a null reference at the line indicated. How can i create an empty structure using nested classes?

using UnityEngine;
using System.Collections;

public class Song : MonoBehaviour {
// Home for the Main Data structure
// Is an Array of Measures
// Each Measure is an array of Chords
// Each Chord is an array of Notes
// Each Note is an Array of Game Objects
private class Measures
{
    public class Chords
    {
        public class Notes
        {
            public GameObject[] Sound;
        }
        public Notes[] Note;
    }
    public Chords[] Chord; 
}

public int MaxMeasures;
public int ChordsPerMeasure;
public int NotesPerChord;
public int MaxObjectsPerNote;

Measures[] Measure;

void Start()
{
    Debug.Log(MaxMeasures); // returns 1
    Measure = new Measures[MaxMeasures+1];
    Debug.Log(Measure.GetUpperBound(0)); // returns 1
    int M = 0;
    for (M = 1; M <= MaxMeasures; M++)
    {
        // next two lines generates null refference error
        Debug.Log(Measure[M].ToString());
        Measure[M].Chord = new Measures.Chords[ChordsPerMeasure+1];
        int C = 0;
        for (C = 1; C <= ChordsPerMeasure; C++)
        {
            Measure[M].Chord
.Note = new Measures.Chords.Notes[NotesPerChord + 1];
                int N = 0;
                for (N = 1; N <= NotesPerChord; N++)
                {
                    Measure[M].Chord[C].Note[N].Sound = new GameObject[MaxObjectsPerNote + 1];
                    int S = 0;
                    for (S = 1; S <= MaxObjectsPerNote; S++)
                    {
                        Measure[M].Chord[C].Note[N].Sound[S] = null;
                    }

                }
            }
        }
    }
}

The null reference exception is because even though you are creating the array, you do not seem to be putting any Measure objects into it. Simply creating the array does not fill it with valid, non-null objects. So you will want to populate the array with Measure objects.

Also, you are setting M to 1 in your for loop, but the first index into the array should be index 0 (which you actually set M to in the line just before the for loop). If you set M to 0 in the for loop then it should process the loop without overrunning the end of the array. You will also run into the same issue with your for loop for C, with the same changes fixing that one as well.

Finally, you are creating the array with a size of maxMeasures + 1, which means your array will hold 1 more Measure object than you are saying you have. You may want to change the array allocation statement to just create an array the size of maxMeasures and then change your for loop to test M < maxMeasures rather than M <= maxMeasures. You also probably want to do the same thing regarding your Chord array and ChordsPerMeasure loop.

If you’re new to programming, getting used to 0 based arrays can take a little time, but overall it’s not too difficult once you get used to starting with 0 instead of 1 for counting things.

I hope that helps. :slight_smile:

Hi
Thank you for taking the time to read my code.
Here is the final script (so far) and it is working

using UnityEngine;
using System.Collections;

public class Song : MonoBehaviour {
// Home for the Main Data structure
// Is an Array of Measures
// Each Measure is an array of Chords
// Each Chord is an array of Notes
// Each Note is an Array of Game Objects
public class Notes
{
    public GameObject[] Sound;
}
public class Chords
{
    public Notes[] Note;
}
public class Measures
{
    public Chords[] Chord;
}

public int MaxMeasures;
public int ChordsPerMeasure;
public int NotesPerChord;
public int MaxObjectsPerNote;

Measures[] Measure;

void Start()
{
    Measures[] NewMeasures = new Measures[MaxMeasures];
    Measure = NewMeasures;
    int M = 0;
    for (M = 0; M < MaxMeasures; M++)
    {
        Measure[M] = new Measures();
        Measure[M].Chord = new Chords[ChordsPerMeasure];
        int C = 0;
        for (C = 0; C < ChordsPerMeasure; C++)
        {
            Measure[M].Chord
 = new Chords();
                Measure[M].Chord[C].Note = new Notes[NotesPerChord];
                int N = 0;
                for (N = 0; N < NotesPerChord; N++)
                {
                    Measure[M].Chord[C].Note[N] = new Notes();
                    Measure[M].Chord[C].Note[N].Sound = new GameObject[MaxObjectsPerNote];
                    int S = 0;
                    for (S = 0; S < MaxObjectsPerNote; S++)
                    {
                        Measure[M].Chord[C].Note[N].Sound[S] = null;
                    }
                }
            }
        }
    }
}