0

I'm having an issue with C# XAML & WinUI3. I am attempting to foreach files:

[ { "path": ".cache", 
    "type": "DIRECTORY" 
  }, 
  { "path": "Cargo.lock" 
  }, 
  { "path": "Cargo.toml" 
  } ] 

and then add them to the MenuItems. I get this error:

Exception thrown: 'System.NullReferenceException' in Project.dll Object reference not set to an instance of an object.

If anyone can help quickly, I would appreciate that.

I tried searching other SO posts & attempting ChatGPT. But nothing helped.

CODE:

    private void AddNavItems(Google.Protobuf.Collections.RepeatedField<Api.File> files)
    {
        foreach (var file in files)
        {
            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);
            });

        }
    }
Flydog57
  • 6,851
  • 2
  • 17
  • 18
avery
  • 11
  • 1
  • 2
  • Either you do not have permission to read the files (could be something else is using the file) or the file could be locked. – jdweng Jul 05 '23 at 03:22
  • What is null (you have debugged this, right?) – Flydog57 Jul 05 '23 at 03:26
  • these are not local files, they are just "files" in json. not real files – avery Jul 05 '23 at 03:27
  • > What is null I did log the files, and it returned properly, if that is what your asking – avery Jul 05 '23 at 03:27
  • You are getting a `NullReferenceException`. That means you are trying to use a variable that is `null`. The exception should tell you what line it's on. Put a breakpoint in your loop and on the first line of the lambda/delegate. Then use the debugger to figure out what's null. Do not be surprised that your question closes as a duplicate of "What is a NullReferenceException". – Flydog57 Jul 05 '23 at 04:15
  • Here's the canonical "how to debug a NullReferenceException link: https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – Flydog57 Jul 05 '23 at 04:21

0 Answers0