title: How to fix System.NullReferenceException: Object reference not set to an instance of an object
body: <p>EDIT: This issue is resolved, good thanks to Reniuz for his 5 hours of work and research of this issue, thank's everyone.</p>
  
  <p>NullReferenceException: Object reference not set to an instance of an object. on the following code and I've searched and searched and pulled my hair out for over 7-8 hours now trying to fix it.</p>
  
  <pre><code>private void buttonAddEffect_Click_1(object sender, EventArgs e)
  {
      EffectSelectorForm effectSelectorForm = new EffectSelectorForm(Effects);
      if (effectSelectorForm.ShowDialog(this) == DialogResult.OK)
      {
          // create a new instance of the selected effect as we may want multiple copies of one effect
          Effect effect = (Effect)Activator.CreateInstance(effectSelectorForm.SelectedEffect.GetType());
          audioGraph.AddEffect(effect);
          int index = checkedListBox1.Items.Add(effect, true);
          checkedListBox1.SelectedIndex = index;
      }
      //MessageBox.Show(String.Format("I have {0} effects", Effects.Count));
  }
  </code></pre>
  
  <p>Error is on the line:
  EffectSelectorForm effectSelectorForm = new EffectSelectorForm(Effects);</p>
  
  <p>the class:</p>
  
  <pre><code>namespace WindowsFormsApplication13
  {
      public partial class EffectSelectorForm : Form
      {
          public EffectSelectorForm(ICollection&lt;Effect&gt; effects)
          {
              InitializeComponent();
              listBoxEffects.DisplayMember = "Name";
              listBoxEffects.DataSource = effects;
          }
  
          private void buttonOK_Click(object sender, EventArgs e)
          {
              this.DialogResult = DialogResult.OK;
              this.Close();
          }
  
          public Effect SelectedEffect
          {
              get
              {
                  return (Effect)listBoxEffects.SelectedItem;
              }
          }
  
          private void listBoxEffects_DoubleClick(object sender, EventArgs e)
          {
              buttonOK_Click(sender, e);
          }
  </code></pre>
  
  <p>What this is supposed to do is when the effectSelectorForm loads it should make a list of all the voice changer options but it does not do that.... it loads nothing on it, I got this code from another form for changing voice in Skype and re-wrote about 400 lines of code for it to work in my app but now I have this issue and I'm not giving up with all the effort I've put in so far. If it loaded on the other project why not in this one? I've went over code for hours over and over thinking im missing something but no.</p>
  
  <p>Any help would be fantastic.</p>
  
  <p>Stack</p>
  
  <pre><code>See the end of this message for details on invoking 
  just-in-time (JIT) debugging instead of this dialog box.
  
  ************** Exception Text **************
  System.NullReferenceException: Object reference not set to an instance of an object.
     at WindowsFormsApplication13.pwn4g3.buttonAddEffect_Click_1(Object sender, EventArgs e) in F:\Users\Tom\Desktop\New folder\New folder (2)\TestApp\pwn4g3\PWN4G3\MainForm2.cs:line 1758
     at System.Windows.Forms.Control.OnClick(EventArgs e)
     at System.Windows.Forms.Button.OnClick(EventArgs e)
     at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
     at System.Windows.Forms.Control.WmMouseUp(Message&amp; m, MouseButtons button, Int32 clicks)
     at System.Windows.Forms.Control.WndProc(Message&amp; m)
     at System.Windows.Forms.ButtonBase.WndProc(Message&amp; m)
     at System.Windows.Forms.Button.WndProc(Message&amp; m)
     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
     at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  </code></pre>
  
  <p>Effect.cs </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  
  namespace JSNet
  {    
  
      public abstract class Effect
      {
          private List&lt;Slider&gt; sliders;
          public float SampleRate { get; set; }
          public float Tempo { get; set; }
          public bool Enabled { get; set; }
  
          public Effect()
          {
              sliders = new List&lt;Slider&gt;();
              Enabled = true;
              Tempo = 120;
              SampleRate = 44100;
          }
  
          public IList&lt;Slider&gt; Sliders { get { return sliders; } }
  
          public Slider AddSlider(float defaultValue, float minimum, float maximum, float increment, string description)
          {
              Slider slider = new Slider(defaultValue, minimum, maximum, increment, description);
              sliders.Add(slider);
              return slider;
          }
  
          public abstract string Name { get; }
  
          // helper base methods
          // these are primarily to enable derived classes to use a similar
          // syntax to JS effects
          protected float slider1 { get { return sliders[0].Value; } }
          protected float slider2 { get { return sliders[1].Value; } }
          protected float slider3 { get { return sliders[2].Value; } }
          protected float slider4 { get { return sliders[3].Value; } }
          protected float slider5 { get { return sliders[4].Value; } }
          protected float slider6 { get { return sliders[5].Value; } }
          protected float slider7 { get { return sliders[6].Value; } }
          protected float slider8 { get { return sliders[7].Value; } }
          protected float min(float a, float b) { return Math.Min(a, b); }
          protected float max(float a, float b) { return Math.Max(a, b); }
          protected float abs(float a) { return Math.Abs(a); }
          protected float exp(float a) { return (float)Math.Exp(a); }
          protected float sqrt(float a) { return (float)Math.Sqrt(a); }
          protected float sin(float a) { return (float)Math.Sin(a); }
          protected float tan(float a) { return (float)Math.Tan(a); }
          protected float cos(float a) { return (float)Math.Cos(a); }
          protected float pow(float a, float b) { return (float)Math.Pow(a, b); }
          protected float sign(float a) { return Math.Sign(a); }
          protected float log(float a) { return (float)Math.Log(a); }
          protected float PI { get { return (float)Math.PI; } }
  
          protected void convolve_c(float[] buffer1, int offset1, float[] buffer2, int offset2, int count)
          {
              for (int i = 0; i &lt; count * 2; i += 2)
              {
                  float r = buffer1[offset1 + i];
                  float im = buffer1[offset1 + i + 1];
                  float cr = buffer2[offset2 + i];
                  float ci = buffer2[offset2 + i + 1];
                  buffer1[offset1 + i] = r * cr - im * ci;
                  buffer1[offset1 + i + 1] = r * ci + im * cr;
              }
          }
  
  
          public virtual void Init()
          {
          }
  
  
          public abstract void Slider();
  
  
          public virtual void Block(int samplesblock)
          { 
          }
  
  
          public abstract void Sample(ref float spl0, ref float spl1);
  
          public override string ToString()
          {
              return Name;
          }
      }
  }
  </code></pre>