C#C
C#3y ago
Avery

❔ Object reference not sent to an instance of an object

I have a WinUI3 app that uses C# and XAML, i'm attempting to foreach files.

Files looks like this:
[ { "path": ".cache", 
    "type": "DIRECTORY" 
  }, 
  { "path": "Cargo.lock" 
  }, 
  { "path": "Cargo.toml" 
  } ] 

(not actual files, just mock data)

But, when I create the NavigationViewItem with code, it gives me
Exception thrown: 'System.NullReferenceException' in MyProject.dll
Object reference not set to an instance of an object.


Code:
                foreach (var file in files)
                {
                    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                    {
                        NavigationViewItem item1 = new NavigationViewItem();
                        item1.Content = file.Path;
                        item1.Tag = file.Path;
                        item1.Icon = new SymbolIcon(file.Type == Api.File.Types.Type.Regular ? Symbol.Page2 : Symbol.Folder);
                        nvSample.MenuItems.Add(item1);
                    });
                }
Was this page helpful?