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();
}