C#C
C#4y 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));
  }
}
And whenever I try to access the following field, I get the following exception:
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')
Was this page helpful?