C
C#6mo ago
Mekasu0124

✅ Item Returning Null From View Model??? Avalonia

MainWindowViewModel: https://pastebin.com/v5A0u9VJ TermsOfServiceViewModel: https://pastebin.com/BQx4qd1D I'm not understanding why when I click the run button to debug and run the program, that's when it deciedes to tell me that the model inside of the MainWindowViewModel's TermsOfService method cannot be null?
94 Replies
Jimmacle
Jimmacle6mo ago
what's the full error? with line numbers etc
Mekasu0124
Mekasu01246mo ago
this is all it gives me but it's not until after I click the run button
No description
Jimmacle
Jimmacle6mo ago
one of those methods has a parameter named source that you're passing null to when you shouldn't (SetupModel)null looks suspect to me and the reason it's "deciding" to tell you it's null at runtime is because it's a runtime error, not a compiler error it looks like you probably have compile time suggestions/hints about nullability, did you look at those?
Mekasu0124
Mekasu01246mo ago
I don't know what those are or what they look like. apologies ok so getting ride of the .Select(_ => (SetupModel)nul)) like fixed it. Thanks for your help
Jimmacle
Jimmacle6mo ago
the green squiggles you should do yourself a favor and add <WarningsAsErrors>Nullable<WarningsAsErrors> to your .csproj properties then code that has sketchy nullability won't compile in the first place
Mekasu0124
Mekasu01246mo ago
where does it go in the csproj file? got it that's making everything error.
internal bool GetSetupValue()
{
var jsonData = JsonSerializer.Deserialize<SetupModel>(_setupPath);
return jsonData.Setup;
}

internal bool UpdateSetup()
{
try
{
SetupModel setup = JsonSerializer.Deserialize<SetupModel>(_setupPath);
setup.Setup = true;

var updatedJsonData = JsonSerializer.Serialize(setup);

File.WriteAllText(_setupPath, updatedJsonData);
return true;
}
catch (Exception ex)
{
return false;
}
}

internal bool UpdateTos()
{
try
{
SetupModel setup = JsonSerializer.Deserialize<SetupModel>(_setupPath);
setup.Tos = true;

var updatedJsonData = JsonSerializer.Serialize(setup);

File.WriteAllText(_setupPath, updatedJsonData);
return true;
}
catch (Exception ex)
{
return false;
}
}
internal bool GetSetupValue()
{
var jsonData = JsonSerializer.Deserialize<SetupModel>(_setupPath);
return jsonData.Setup;
}

internal bool UpdateSetup()
{
try
{
SetupModel setup = JsonSerializer.Deserialize<SetupModel>(_setupPath);
setup.Setup = true;

var updatedJsonData = JsonSerializer.Serialize(setup);

File.WriteAllText(_setupPath, updatedJsonData);
return true;
}
catch (Exception ex)
{
return false;
}
}

internal bool UpdateTos()
{
try
{
SetupModel setup = JsonSerializer.Deserialize<SetupModel>(_setupPath);
setup.Tos = true;

var updatedJsonData = JsonSerializer.Serialize(setup);

File.WriteAllText(_setupPath, updatedJsonData);
return true;
}
catch (Exception ex)
{
return false;
}
}
every json serialization and it's usage beneath it is now an error
Jimmacle
Jimmacle6mo ago
then you have a lot of sketchy nullability as in, things that could be null that your code doesn't check for
Mekasu0124
Mekasu01246mo ago
No description
Mekasu0124
Mekasu01246mo ago
No description
Jimmacle
Jimmacle6mo ago
yes deserializing json may return null, but your code doesn't check for it
Mekasu0124
Mekasu01246mo ago
they're all the same respectively so how do I fix it?
Jimmacle
Jimmacle6mo ago
which is a potential runtime crash by adding null checks
Mekasu0124
Mekasu01246mo ago
ok I'll go research that
Mekasu0124
Mekasu01246mo ago
so googling this, I found https://www.thomasclaudiushuber.com/2020/03/12/c-different-ways-to-check-for-null/ is that what you're talking about?
Thomas Claudius Huber
Thomas Claudius Huber
C#: Different ways to Check for Null
What is the classic way to check if for example a parameter value is null? If you've developed with C# since a while, you might be familiar with this classic syntax: public static int CountNumberOfSInName(string name) { if (name == null) { throw new ArgumentNullException(nameof(name));…
Mekasu0124
Mekasu01246mo ago
if yes, then that means I need to build some type of universal error and exception handler
Want results from more Discord servers?
Add your server