views:

200

answers:

6

I run into a very strange problem in my C# 2.0 WinForms app and I'm not even sure if its worth asking SO, because the problem occurs in a strange setup and I don't think that you could reproduce it without my sources, but I'm totally out of ideas.

I have a Form with a TreeView on the left and an ListView on the right. The TreeView shows all available files and subfolders from a specific folder(which contains documents i need for my app). If a Folder is selected the ListView shows all files and subfolders from the selected folder. At startup I populate the TreeView form the folder and after that I select the first TreeNode by code(in my case it's an folder). After that the Content of the TreeView looks like this:

-folder
   -file1
   -file2

Selecting the folder triggers the AfterSelecedEvent of the TreeView. Because a folder was selected I populate the ListView using the following methode:

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 the line item = new ListViewItem(file.Name, "Folder"); should never be touched in this setup, but every now and then a NullReferenceException got thrown. If I wrap this line with try/catch the exception got thrown inside the catch block. I tried checking everything if it's null or not, but ther were no nullreferences. Or if I add a MessageBox right before this line the exceptions got still thrown and no MessageBoxpops up. This brings me to the conclusion that the execption stacktrace is wrong and/or this exceptions comes from an other Thread or something like that.

I'm a very optimistic person and I know how clever the SO community can be, but I don't think that anybody can point out what the problem is. So what i'm actuallly looking for are hints and advices how i could find and debug the cause of this strange behavior.

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);
    }
}
A: 

Have you tried an null check on folder.sortedContent ?

Usually ReSharper will prompt me that something like that should have a null check.

If you want to be sure, add the following line to your code, above the foreach loop:

if (folder.sortedContent == null) throw new Exception("It was null, dangit!");
Josh Pearce
no, the sortedContent allways returns a `new List()`
Marcel Benthin
A: 

On the line you mention:

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

the only thing that can cause a NullReferenceException is if file is null (unless the exception is being thrown from within the ListViewItem constructor itself).

You don't provide the code for folder.sortedContent so I can't tell - but is it possible that one of the elements in this collection might be null under certain circumstances?

If the ListViewItem constructor is throwing the exception then you will need to use Reflector to look at the code, or download the reference source.

RichTea
`file` can't be `null` because of the `if (file is FAFolder)`. `null is object` is allways `false` and `sortedContent` allways returns a `new List()`. But lets assumed `sortedContent == null`, than the body of the `foreach` whouldn't be executed.
Marcel Benthin
A: 

A co-worker of mine just found the answer(probably). I use a Thread to load the ImageList to the ListView from the HDD and this thread sometimes freezes and if i assign a ImageKey it fails. That's no answer why the exception is uncatchable or why it's thrown at this (unreachable) line. But i strongly belive that this is the cause of the problem.

Marcel Benthin
A: 

That line is not unreachable. Because FAFolder derives from FAFile, it is possible that 'file is FAFolder' will return true.

However, that would imply that file is not null, unless it is being changed by another thread.

Edit: file can't be changed by another thread as it's a local reference. Can you provide a stack trace for the exception? This one has me intrigued now.

pdr
A: 

I just cannot help wondering is the FAFolder contain a '.' or a '..' for parent and subdirectory? and the sorting breaks as a result?

This answer will be edited accordingly if this turns out to be untrue?

Hope this helps, Best regards, Tom.

tommieb75
A: 
  • Is this exception reproducible on demand?
  • Can you show the the stack trace of the exception?
  • What other threads are running when the exception is thrown?

In general, there are two ways to debug this type of stuff. The first way is so-called "scientific" debugging:

  1. Devise a theory to explain observed behaviour.
  2. Devise an experiment to test the theory.
  3. Run the experiment and observe the results.

The second way is by stripping-down the actual code piece by piece until the exception is no longer triggered. Then you have a significant clue for further investigation.

This brings me to the conclusion that the execption stacktrace is wrong....

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

RoadWarrior