0

I don't really understand why everything is fine in the if block, even if textboh null, but the same conditions but entering the else block cause an error

private async void OpenBug()
{
    if (File.ReadLines("Response.json").First() == null)
    {
        HttpClient _client = new HttpClient();
        HttpResponseMessage response = new HttpResponseMessage();
        var authstring = "auth";
        _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authstring);
        var confSearch = await _client.GetAsync("get");
        var json = await confSearch.Content.ReadAsStringAsync();
        dynamic media = JObject.Parse(json);
        string total = media.total;
        string fileName = "Response.json";
        File.WriteAllText(fileName, total);
        string readtotal = File.ReadLines(fileName).First();
        OpenBugs.Text = readtotal;
    }
    else
    {
        string fileName = "Response.json";
        string readtotal = File.ReadLines(fileName).First();
        OpenBugs.Text = readtotal;
    }
}

I tried doing additional if with String.IsNullOrWhiteSpace and String.IsNullOrEmpty in the else block, but it doesn't help

Firo
  • 30,626
  • 4
  • 55
  • 94
Resonance
  • 1
  • 1
  • probably `OpenBugs` is `null`. – MakePeaceGreatAgain Mar 02 '23 at 07:24
  • The expression `if (File.ReadLines("Response.json").First() == null)` is a programming error. If the file is empty `First` throws an InvalidOperationException. Besides that, do not call `File.ReadLines(fileName)` multiple times. Keep the returned string array in a variable. – Clemens Mar 02 '23 at 08:07
  • Also declaring a method that is not an event handler as `async void` is an error. It has to be `async Task` and the call has to be awaited, like `await OpenBug();`. – Clemens Mar 02 '23 at 08:09

0 Answers0