string[] directories = textBox5.Text.Split(new Char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
FileSystemWatcher[] fileSysWatchers = new FileSystemWatcher[directories.Length];
for (int i = 0; i < directories.Length; i++)
{
directories[i] = directories[i].Replace("\n", "");
directories[i] = directories[i].Replace("\r", "");
fileSysWatchers[i].Path = directories[i];
if (checkBox8.Checked)
{
fileSysWatchers[i].Created += new FileSystemEventHandler(Form1_Created);
}
}
I’m getting NullReferenceException in the next line:
fileSysWatchers[i].Path = directories[i];
I’ve checked directories[0] and found that it has no .Path but I don’t understand why, it has to.
Basically, I want to create a specific FileSystemWatcher for each directory in some list and handle all their “Created” events using only one EventHandler. If there is some way to watch over multiple directories using only one FileSystemWatcher, I want to know that.
Sorry for my English if it’s not perfect.
You have only allocated an array capable of holding FileSystemWatcher instance but not the instances themselves.
You are missing the following line (before the one that crashes):
It’s not
Paththat isnull, it’s the FileSystemWatcher instance itself.