0

I am creating this MVC app and I have this same code below for another method different naming/wordings. However, this method is always throwing this error:

System.ArgumentNullException: Value cannot be null.

Parameter name: source

I have searched everywhere but no luck in solving this:

The code from my controller is:

    public List<Quote> GetQuote(string symbol1) //this action method returns the quote API endpoint 
    {
        // string to specify information to be retrieved from the API
        string IEXTrading_API_PATH = BASE_URL + "stock/" + symbol1 + "/quote";

        // initialize objects needed to gather data
        string CompanyQuote = "";
        List<Quote> DailyQuote = new List<Quote>();
        httpClient.BaseAddress = new Uri(IEXTrading_API_PATH);

        // connect to the API and obtain the response
        HttpResponseMessage response = httpClient.GetAsync(IEXTrading_API_PATH).GetAwaiter().GetResult();

        // now, obtain the Json objects in the response as a string
        if (response.IsSuccessStatusCode)
        {
            CompanyQuote = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
        }

        // parse the string into appropriate objects
        if (!CompanyQuote.Equals(""))
        {
            QuoteRoot quoteroot = JsonConvert.DeserializeObject<QuoteRoot>(CompanyQuote,
              new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
            DailyQuote = quoteroot.quote.ToList();
        }

        // fix the relations. By default the quotes do not have the company symbol
        //  this symbol serves as the foreign key in the database and connects the quote to the company
        foreach (Quote quotee in DailyQuote)
        {
            quotee.companysymbol = symbol1;
        }

        return DailyQuote;
    }

The line returning this error is at this block:

    // parse the string into appropriate objects
    if (!CompanyQuote.Equals(""))
    {
        QuoteRoot quoteroot = JsonConvert.DeserializeObject<QuoteRoot>(CompanyQuote,
          new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
        DailyQuote = quoteroot.quote.ToList();
    }
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
bonashhh
  • 23
  • 1
  • 5
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – ProgrammingLlama Apr 08 '19 at 05:43
  • You're performing a LINQ operation on a null object. presumably `quoteroot` or `quoteroot.quote` is `null` (the LINQ operation is `.ToList()`) – ProgrammingLlama Apr 08 '19 at 05:44
  • Also, please make sure you read the descriptions of tags before you use them so as not to incorrectly tag your question. I removed the `visual-studio` tag for you. – ProgrammingLlama Apr 08 '19 at 05:45
  • It is saying `quoteroot.quote` is null but data is returned from the API. as a result `DailyQuote` is not getting any value – bonashhh Apr 08 '19 at 05:52
  • If `response.IsSuccessStatusCode` is `true` then you'll have to do some digging to find out why. Without access to the same API and the exact same request that you're making along with data classes, it's not really possible to advise why. JSON.NET will sometimes make things null if the types in the JSON aren't compatible with types in .NET, but it will usually throw an exception. – ProgrammingLlama Apr 08 '19 at 05:54

1 Answers1

0

I guess the following part will throw the exception:

    if (response.IsSuccessStatusCode)
    {
        CompanyQuote = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
    }

Please check for Content NULL:

    if (response.IsSuccessStatusCode && response.Content != null)
    {
        CompanyQuote = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
    }
Markus Meyer
  • 3,327
  • 10
  • 22
  • 35