Being new to c#, I don't understand how variables are passed between objects. My array variable "filePaths" is coming back null when I exectute this program. It is a basic windows form. I'm working on making a program that will show the words and play the sound.
The specific error is "NullReferenceException was unhandled.
Here is my particular code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Media;
namespace Kindersect
{
public partial class form1 : Form
{
string[] filePaths;
string directpath = "C:\\Users\\Optimus Prime\\Documents\\vocabaudio\\";
int counter = 0;
int c = 0;
public form1()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
string[] filePaths = Directory.GetFiles(directpath, "*.wav");
foreach(string k in filePaths)
{
c++;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (counter < c)
{
label1.Text = filePaths[counter];
SoundPlayer simpleSound = new SoundPlayer(filePaths[counter]);
simpleSound.Play();
counter++;
}
}
}
}
Thanks in advance.