0

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.

2
  • 1
    is optimus prime using your computer too? that s cool man. Commented Jul 4, 2012 at 20:08
  • Thanks everybody for explaining both the declaration issues I had plus the character escaping issues I had. My newbness betrays me. Commented Jul 4, 2012 at 22:47

5 Answers 5

3

You are declaring two different variables... in different scopes

remove string[] from the second declaration of filepath if you want access to the global declared filepath

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer that works, and for the explanation so I understand. I'm now off to my next mistake...
2

Lose the @'s when referencing your variables.

You're also declaring filePaths twice. Once in the class (and is never defined) and once in the button click event handler that goes out of scope in that method. You only want to declare it in the class and set it in the method so remove the string[] from the line in the method.

1 Comment

ok, it doesn't help, but sure. Any idea on getting the value created on the button click to be readable under the timer?
0

First: you shouldn't start timer before setting the variable.

Two: you don't have to redefine type of variable if defined at the beginning.

Comments

0

It looks like you are using @ symbol incorrectly. The @ symbol in front of a string or string reference is meant to disable the escaping feature of backslashes (\). Normally you have to escape backslashes with an additional backslash like you currently have (\\).

So...

string directpath = "C:\\Users\\Optimus Prime\\Documents\\vocabaudio\\";

is equivalent to

string directpath = @"C:\Users\Optimus Prime\Documents\vocabaudio\";

See also : @(at) sign in file path/string

Comments

0

The problem i can see in your code is declare string[] filePaths; at class level and then use it in timer1_Tick Event but string[] filePaths; never get values assigned to it because you have a similar name variable inside button1_Click on line : string[] filePaths = Directory.GetFiles(@directpath, "*.wav"); but the scope of this filePaths array is just inside button1_Click only

So to resolve your issue please change

string[] filePaths = Directory.GetFiles(@directpath, "*.wav");

to 

filePaths = Directory.GetFiles(@directpath, "*.wav");

I would suggest you use your methods this way more smaller and clear code with less variables :

public void button1_Click(object sender, EventArgs e)
{
    timer1.Enabled = true;
}

    private void timer1_Tick(object sender, EventArgs e)
    {
        filePaths = Directory.GetFiles(directpath, "*.wav");
        if (counter < filePaths.Length)
        {
            label1.Text = filePaths[counter];
            SoundPlayer simpleSound = new SoundPlayer(filePaths[counter]);
            simpleSound.Play();
            counter++;
        }

    }

If you can use Directory.GetFiles in Form_Load event this way it will get called just once

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.