1

I wrote code which creates a list of strings based on the names of the files in a directory. I am struggling to display elements from the List of strings to XAML label. I can see in debug that the list populates correctly but when I try to output the names of the files to the XAML label I get an error: System.NullReferenceException: 'Object reference not set to an instance of an object.' when the compiler gets to the point where it needs to output the values into the label

As I am fairly new to the C# and XAML I am probably not doing it right, but I cannot figure on my own how to do it?

Here is part of my XAML code:

 <Label x:Name="lbTSMDisplayNames" Content=""/>

Here is the C# code:

        var listPictures = new List<string>();
        DirectoryInfo dinfo = new DirectoryInfo(@"C:\Resources\images_test");
        foreach (FileInfo inf in dinfo.GetFiles())
        {
            listPictures.Add(inf.FullName);
        }
        foreach (string pict in listPictures)
            lbTSMDisplayNames.Content += pict;

I would like to display the names of the files in the label.

At the moment I am getting the following error: System.NullReferenceException: 'Object reference not set to an instance of an object.'

Behemot
  • 73
  • 1
  • 5
  • I assume your C# code runs before InitializeComponent has been called. Make sure it runs after InitializeComponent() – less Jul 11 '19 at 09:06

0 Answers0