I ran into a very strange problem in my C # 2.0 WinForms application, and I'm not even sure whether to ask it, because the problem arises in a strange setup, and I donβt think you could reproduce it without my sources, but I completely out of ideas.
I have a form with TreeViewleft and ListViewright. TreeViewshows all available files and subfolders from a specific folder (which contains the documents that I need for my application). If a folder is selected, ListViewdisplays all files and subfolders from the selected folder. When I start, I fill out the TreeView form, and after that I select the first one TreeNodeby code (in my case, this is a folder). After that, the contents of the TreeView are as follows:
-folder
-file1
-file2
Selecting a folder calls up AfterSelecedEvent TreeView. Since the folder was selected, I fill ListViewusing the following method:
private void fillOverview(FAFolder folder)
{
lv_overview.Items.Clear();
ListViewItem item;
foreach (FAFile file in folder.sortedContent)
{
if (file is FAFolder)
{
item = new ListViewItem(file.Name, "Folder");
}
else
{
item = new ListViewItem(file.Name, file.Name);
}
item.Tag = file;
lv_overview.Items.Add(item);
}
}
As you can see, there is no subfolder, so in this setting you should never touch the line item = new ListViewItem(file.Name, "Folder");, but it is thrown from time to time NullReferenceException. If I end this line with try / catch, the exception is caught in the catch block. I tried to check everything if it is nullor not, but they were not null references. Or, if I add MessageBoxright before this line, exceptions are still thrown and not appearing MessageBox. This leads me to conclude that execptrace execption is erroneous and / or this exception comes from another Threador something similar.
, , SO, , - , . , , , .
EDIT:
internal abstract class FAFile
{
internal string Name;
internal readonly FAFolder Parent;
internal FAFile(FAFolder parent)
{
this.Parent = parent;
}
}
internal sealed class FAFolder : FAFile
{
internal readonly IDictionary<string, FAFile> Content = new Dictionary<string, FAFile>();
internal FAFolder(FAFolder parent, string name) : base(parent)
{
this.Name = name;
}
}
internal sealed class FADocument : FAFile
{
public readonly string Path;
public FADocument(FAFolder parent, string path): base(parent)
{
this.Path = path;
this.Name = System.IO.Path.GetFileNameWithoutExtension(path);
}
}