C
C#2y 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"
} ]
[ { "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.
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);
});
}
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);
});
}
23 Replies
HimmDawg
HimmDawg2y ago
You got a line for us?
Avery
AveryOP2y ago
What do you mean? Like; where it's erroring?
HimmDawg
HimmDawg2y ago
Exactly
Avery
AveryOP2y ago
I set some breakpoints, it said it's where i'm doing new NavigationViewItem
HimmDawg
HimmDawg2y ago
huhcat new NavigationViewItem()? That's.... interesting
Avery
AveryOP2y ago
yeah
HimmDawg
HimmDawg2y ago
I'd say it is because you run it asynchronously in the Dispatcher
Avery
AveryOP2y ago
I have to do that, because it won't let me create the nav items because different thread or whatever
HimmDawg
HimmDawg2y ago
Different thread? You can usually add MenuItems as easy as
var item = new NavigationViewItem()
{
Content = "Foo",
Tag = "Bar"
// ...
};
NavigationView.MenuItems.Add(item);
var item = new NavigationViewItem()
{
Content = "Foo",
Tag = "Bar"
// ...
};
NavigationView.MenuItems.Add(item);
Avery
AveryOP2y ago
let me try that, with out the dispatcher
Avery
AveryOP2y ago
HimmDawg
HimmDawg2y ago
Okay, but that means you are running the entire function in a different thread.
Avery
AveryOP2y ago
yeah, when I receive a message from the websocket, it adds the item that's my intention if it's possible
HimmDawg
HimmDawg2y ago
Can we see more code? What I'd think is, that the NavigationViewItems should be created in the same thread, where the NavigationView was created and your socket just passes you the data for it
Avery
AveryOP2y ago
Yeah, i'm creating the navigation view in the actual XAML. Not in the code. https://hastebin.skyra.pw/digojikiya.csharp had to find a hastebin service to upload the code to. too big for discord this is nvSample_Loaded_1
HimmDawg
HimmDawg2y ago
Hummm, in that case, passing data will be more difficult. Can you try the same approach with Dispatcher as before, but use
DispatcherQueue.TryEnqueue(() =>
{
// create and add NavItems
});
DispatcherQueue.TryEnqueue(() =>
{
// create and add NavItems
});
Avery
AveryOP2y ago
holy, it works now tysm! everything works now, but i'm not sure if you'll be able to help me with another error.
private async void MonacoEditor_NavigationCompleted(WebView2 sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs args)
{
await Task.Delay(500); // Use asynchronous delay
System.Diagnostics.Debug.WriteLine(fileContents);
try
{
await MonacoEditor.ExecuteScriptAsync("monaco.editor.getModels()[0].setValue(\"" + fileContents + "\");");
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Script execution error: " + ex.Message);
}

System.Diagnostics.Debug.WriteLine("possibly set value");
}
private async void MonacoEditor_NavigationCompleted(WebView2 sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs args)
{
await Task.Delay(500); // Use asynchronous delay
System.Diagnostics.Debug.WriteLine(fileContents);
try
{
await MonacoEditor.ExecuteScriptAsync("monaco.editor.getModels()[0].setValue(\"" + fileContents + "\");");
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Script execution error: " + ex.Message);
}

System.Diagnostics.Debug.WriteLine("possibly set value");
}
The file contents logs, and the "possibly set value" also logs. But, the monaco script doesn't work. When I manually execute the JS in the devtools console, that appears to work; but the executescriptasync isn't doing anything. I did the task delay to wait for the editor to load once navigation was completed.
HimmDawg
HimmDawg2y ago
Ah, I never worked with MonacoEditor. So I probably cannot help
Avery
AveryOP2y ago
MonacoEditor, is the WebView2; sorry
HimmDawg
HimmDawg2y ago
Do you have any way to check if the script was executed correctly?
Avery
AveryOP2y ago
the value doesn't set the contents
HimmDawg
HimmDawg2y ago
Well, I am not sure what the script is referring to altogether. No clue what "monaco.editor.getModels()" is doing 😄. I cant help any further
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server