Elusive Exception from Unreachable Code

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"); //exception got thrown here
        }
        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);
    }
}
+3
6

(). Thread ImageList ListView HDD, , ImageKey, . , () . , .

0

folder.sortedContent?

ReSharper , - .

, foreach:

if (folder.sortedContent == null) throw new Exception("It was null, dangit!");
0

:

item = new ListViewItem(file.Name, "Folder")

, NullReferenceException, file null ( ListViewItem).

folder.sortedContent, - , null ?

ListViewItem , Reflector .

0

. FAFolder FAFile, , " FAFolder" true.

, , .

: , . ? .

0

, FAFolder ".". ".." ? ?

, ?

, , , .

0
  • ?
  • ?
  • ?

, . - "" :

  • Come up with a theory to explain observed behavior.
  • Imagine an experiment to test the theory.
  • Run the experiment and see the results.

The second way is to hide the actual piece of code in parts until the exception is no longer triggered. Then you have a substantial hint for further study.

This leads me to conclude that the execption stop code is incorrect ....

It is usually easier to start by assuming that the problem is in your own code .

0
source

Source: https://habr.com/ru/post/1733344/


All Articles