Crosspost from here: http://stackoverflow.com/questions/8461941/naudio-nullreferenceexception-when-using-a-start-stop-button
I have a GUI form with a checkbox button for Start/Stop. When the program starts, clicking Start and Stop works once. When I try and click Start again after I stop the recording, I get a NullReferenceException:
An unhandled exception of type 'System.NullReferenceException' occurred in NAudio.dll Additional information: Object reference not set to an instance of an object.
at this line in my Program.cs file:
Application.Run(new GUI());
Here is my Start/Stop button in my GUI form:
public GUI() { InitializeComponent(); InitializeAudio(); } private void btnStartStop_CheckedChanged(object sender, EventArgs e) { if (btnStartStop.Checked) { waveInDevice.StartRecording(); waveOutDevice.Play(); btnStartStop.Text = "Stop"; } else { btnStartStop.Text = "Start"; waveInDevice.StopRecording(); waveOutDevice.Stop(); } } private void InitializeAudio() { buffer = new BufferedWaveProvider(waveInDevice.WaveFormat); buffer.DiscardOnBufferOverflow = true; waveInDevice.DataAvailable += new EventHandler<WaveInEventArgs>(waveInDevice_DataAvailable); waveOutDevice.Init(buffer); }
Could anyone have any advice as to why this is happening?