ArgumentNullException when argument is not null
I have this code:
And whenever I try to access the following field, I get the following exception:
22 Replies
which line does it get thrown at?
in the constructor
How do I see that?
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
It's at
File.Create(_fileName).Close();
but in general, reading files in a constructor that's called in static context might not be the best idea
When I hover over _fileName, it says it isn't null, but that's where it throws the exception
a constructor reading files is already cursed to me
So I should move it to a method?
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
It's a different constructer
which is absolutely cursed
you sure it calls private constructirs by default?
That's the only parameterless constructer
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
Oh wait it works now without making it public
By moving it to a method
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
Was the constructer called in a static context only cursed because it was reading a file, or is that still cursed
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
My Main method switches to an async task which is where the config is used
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
So for every class that needs the config, I should make it into a non-static field and load it in the constructer?
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
Okay thanks
!solved