-1

I'm trying to build a 3D Spectograph and get a bunch of frequency data for a given point in time. I need to simplify this in order to be able to handle the instantiating of a lot of prefabs. So I use a fixed count of Rows of fequency data to look at:

int spectrumRows = 24;

I then call this function to "simplify" the detail of the frequency spectrum by adding values together and then just adding their median to a float array called "floatArray". This then is passed to another float array, "simpleSpectrum", which tells me that there is this reference error.

public void addToSimpleSpectrum(float[] spectrum) {
    float spectralsInRow = spectrum.Length / spectrumRows;
    Math.Round(spectralsInRow);
    float[] floatArray = new float[(int)spectralsInRow];
    for(int i = 0; i < spectralsInRow; i++){    
        float median = 0f;
        float sum = 0f;
        for (float o = spectralsInRow*i; o < (spectralsInRow*(i+1)); o++) {
            sum = sum + spectrum[(int)o];
        }
        median = sum / spectralsInRow;  
        floatArray[i] = median;
    }   
    simpleSpectrum.Add(floatArray);
}

The error I'm getting in Unity is:

System.NullReferenceException: Object reference not set to an instance of an object at [...]:46

Which means that the "simpleSpectrum.Add(floatArray);" doesn't work because it doesn't know what the floatArray is. The "public List<float[]> simpleSpectrum" is created in global scope. But the floatArray should be accessible within the function, or not?

I hope somebody can help me. C# is new to me!

etlaM
  • 131
  • 1
  • 1
  • 9
  • 2
    `Math.Round(spectralsInRow);` is a no-op as you are not saving the result. `simpleSpectrum` needs an assigned value somewhere `= new List()`. I suggest you read up on how assignments work. – Charlieface Jan 04 '21 at 23:33
  • 3
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Charlieface Jan 04 '21 at 23:33
  • Also, after rounding `spectralsInRow` you can store it in an `int` to save converting it every time – Charlieface Jan 04 '21 at 23:34
  • Hang on there. The error means that the object being referenced, which is to say `simpleSpectrum`, is null. It has nothing to do with the value of `floatArray`. – Ruzihm Jan 04 '21 at 23:58

1 Answers1

1

Please make it:

private  List<float[]> _simpleSpectrum = new List<float>();

Or if you need to access it outside the class:

public List<float[]> SimpleSpectrum { get; set; }

This way it will a) work and b) be aligned with standard C# naming convention

passed to another float array, "simpleSpectrum"

Be careful using the words "array" and "list" - other developer professionals will expect you to really actually be using those things if that's what you say. SimpleSpectrum is a list, not an array. A fellow C# dev would be very confused if you insisted it was an array and then showed some code where you called an Add method on it

Caius Jard
  • 72,509
  • 5
  • 49
  • 80