C
C#3y ago
Bujju

ArgumentNullException when argument is not null

I have this code:
public class Config
{
[JsonProperty("token")]
public string Token = "TOKEN HERE";

[JsonProperty("source_url")]
public string SourceUrl = "SOURCE URL HERE";

[JsonProperty("server_url")]
public string ServerUrl = "SERVER URL HERE";

[JsonProperty("invite_url")]
public string InviteUrl = "INVITE URL HERE";

[JsonIgnore]
private string _fileName;

public Config(string fileName)
{
_fileName = fileName;

if (!File.Exists(_fileName)) File.Create(_fileName).Close();

var loaded = JsonConvert.DeserializeObject<Config>(File.ReadAllText(_fileName));
if (loaded is not null)
{
Token = loaded.Token;
SourceUrl = loaded.SourceUrl;
ServerUrl = loaded.ServerUrl;
InviteUrl = loaded.InviteUrl;
}
else
{
File.WriteAllText(_fileName, JsonConvert.SerializeObject(new Config(), Formatting.Indented));
}
}

private Config()
{
_fileName = string.Empty;
}

public void Update()
{
File.WriteAllText(_fileName, JsonConvert.SerializeObject(this, Formatting.Indented));
}
}
public class Config
{
[JsonProperty("token")]
public string Token = "TOKEN HERE";

[JsonProperty("source_url")]
public string SourceUrl = "SOURCE URL HERE";

[JsonProperty("server_url")]
public string ServerUrl = "SERVER URL HERE";

[JsonProperty("invite_url")]
public string InviteUrl = "INVITE URL HERE";

[JsonIgnore]
private string _fileName;

public Config(string fileName)
{
_fileName = fileName;

if (!File.Exists(_fileName)) File.Create(_fileName).Close();

var loaded = JsonConvert.DeserializeObject<Config>(File.ReadAllText(_fileName));
if (loaded is not null)
{
Token = loaded.Token;
SourceUrl = loaded.SourceUrl;
ServerUrl = loaded.ServerUrl;
InviteUrl = loaded.InviteUrl;
}
else
{
File.WriteAllText(_fileName, JsonConvert.SerializeObject(new Config(), Formatting.Indented));
}
}

private Config()
{
_fileName = string.Empty;
}

public void Update()
{
File.WriteAllText(_fileName, JsonConvert.SerializeObject(this, Formatting.Indented));
}
}
And whenever I try to access the following field, I get the following exception:
private static Config Config = new("config.json");
private static Config Config = new("config.json");
System.TypeInitializationException: 'The type initializer for 'class' threw an exception.'

Inner Exception:
ArgumentNullException: Path cannot be null. (Parameter 'path')
System.TypeInitializationException: 'The type initializer for 'class' threw an exception.'

Inner Exception:
ArgumentNullException: Path cannot be null. (Parameter 'path')
22 Replies
Anton
Anton3y ago
which line does it get thrown at? in the constructor
Bujju
Bujju3y ago
How do I see that?
Anton
Anton3y ago
the debugger should show you try instantiating the config in normal flow, like in the main method, if it doesn't show the line the fact that it's statically initialized like that might be confusing it
Bujju
Bujju3y ago
It's at File.Create(_fileName).Close();
Anton
Anton3y ago
but in general, reading files in a constructor that's called in static context might not be the best idea
Bujju
Bujju3y ago
When I hover over _fileName, it says it isn't null, but that's where it throws the exception
Anton
Anton3y ago
a constructor reading files is already cursed to me
Bujju
Bujju3y ago
So I should move it to a method?
Anton
Anton3y ago
absolutely that however is not the problem here you sure it doesn't fail on the second line? where you deserialize ah hold on when deserializing it calls the constructor again probably with a null argument
Bujju
Bujju3y ago
It's a different constructer
Anton
Anton3y ago
which is absolutely cursed you sure it calls private constructirs by default?
Bujju
Bujju3y ago
That's the only parameterless constructer
Anton
Anton3y ago
it bet it favors public ones if there are any and tries to match parameters by name or passes nulls try making it public it might actually just work
Bujju
Bujju3y ago
Oh wait it works now without making it public By moving it to a method
Anton
Anton3y ago
did you turn that into a method? ok delay, sorry so your deserializer most likely favored the public constructor, even though there was a parameterless one just because it was private, the serializer decided to ignore it and now that there's no public constructors, it's forced to use the only available one
Bujju
Bujju3y ago
Was the constructer called in a static context only cursed because it was reading a file, or is that still cursed
Anton
Anton3y ago
I wouldn't make such things like config static at all. I'd load them in my main or wherever, and pass around explicitly or via dependency injection Reading a file in a constructor is definitely cursed
Bujju
Bujju3y ago
My Main method switches to an async task which is where the config is used
Anton
Anton3y ago
storing config in a static variable — somewhat cursed, but ok for one-off apps or if you're just learning the basics I'd load the config, then pass the config to the task why should the task touch globals at all? if you can make the dependency on that config explicit, do it explicit dependencies are a good thing. they are more verbose, but they make your program a lot more tractable in the long run
Bujju
Bujju3y ago
So for every class that needs the config, I should make it into a non-static field and load it in the constructer?
Anton
Anton3y ago
plus, if many tasks try to read that global at once, I think class initialization is under a lock, so they'd collectively sit and wait on the IO of loading the config from disk for no reason, blocking the worker threads, potentially wasting resources not load, pass it in as a parameter the loading shouldn't be done in the constructor in general, try to do the least work you can in the constructor the best constructor is one that just assigns fields
Bujju
Bujju3y ago
Okay thanks !solved
Want results from more Discord servers?
Add your server