Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Object reference not set to an instance of an object

Discussion in 'Scripting' started by Eric_Lin, Dec 23, 2014.

  1. Eric_Lin

    Eric_Lin

    Joined:
    Oct 29, 2014
    Posts:
    32
    Hi
    I've been doing some examples of loading a song and trying to detect its beats. Now I am getting there thanks to some articles I found online. Here is my question, while trying to get access to my own class FFT I got the error message as described on topic. Can someone tell me why this is happening? Thanks in advance. Here is my code, have a look.


    ----------------------------------------------------------------------------------
    usingUnityEngine;
    usingSystem.Collections;

    publicclassspec2 : MonoBehaviour
    {
    publicAudioSourcesong; //settheaudiosourcetodetect
    float[] channelRight;
    float[] channelLeft;
    publicclassFFTElement
    {
    publicfloatre = 0.0f; //Realcomponent
    publicfloatim = 0.0f; //Imaginarycomponent
    publicFFTElementnext; //pointtonextelement
    }

    FFTElement[] _data = newFFTElement[1024];

    voidStart ()
    {
    //allocateelementforlinkedlist
    for(inti=0;i<1023;i++)
    {
    _data = newFFTElement();
    }
    //setupnextpointer
    for(inti=0;i<1023;i++)
    {
    _data.next = _data[i+1];
    }
    }
    voidUpdate ()
    {
    for(intj=0;j<channelLeft.Length;j++)
    {
    channelRight = song.audio.GetSpectrumData(1024,0,FFTWindow.BlackmanHarris);
    channelLeft = song.audio.GetSpectrumData(1024,1,FFTWindow.BlackmanHarris);
    //computetheinstantsoundenergy
    e = e + ((channelLeft[j]*channelLeft[j]) + (channelRight[j]*channelRight[j]));
    historyBuffer = e; //inserteintobuffertostore
    _data[j].re = channelRight[j];
    _data[j].im = channelLeft[j];
    }
    }
    }
     
  2. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    1. Use CODE Tags
    2. Please post the error message and stack trace
    3. It means exactly what it says. You are trying to access an object and you haven't created an instance of it (it's null).
     
    Eric_Lin likes this.
  3. Eric_Lin

    Eric_Lin

    Joined:
    Oct 29, 2014
    Posts:
    32
    Hi, Dustin
    Sorry for the mistakes i made here, and thanks for telling me. Because i am beginner here can you tell me how to use code tag? And the error message is "Object reference not set to an instance of an object". And it told me that the error happened while running _data[j].re = channelRight[j]. I thought I did declare and new the class, but i didn't know how can't I use it.