首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何修复System.ArgumentNullException:值不能为空

如何修复System.ArgumentNullException:值不能为空
EN

Stack Overflow用户
提问于 2019-04-08 13:41:13
回答 1查看 1.1K关注 0票数 0

我正在创建这个MVC应用程序,我有下面的相同代码,用于另一种方法不同的命名/措辞。然而,这个方法总是抛出这个错误:

代码语言:javascript
运行
复制
System.ArgumentNullException: Value cannot be null.

Parameter name: source

我找遍了所有地方,但没有找到答案:

来自我的控制器的代码是:

代码语言:javascript
运行
复制
    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;
    }

返回此错误的行在下面的代码块中:

代码语言:javascript
运行
复制
    // parse the string into appropriate objects
    if (!CompanyQuote.Equals(""))
    {
        QuoteRoot quoteroot = JsonConvert.DeserializeObject<QuoteRoot>(CompanyQuote,
          new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
        DailyQuote = quoteroot.quote.ToList();
    }
EN

回答 1

Stack Overflow用户

发布于 2021-02-19 12:33:44

我猜下面的部分会抛出异常:

代码语言:javascript
运行
复制
    if (response.IsSuccessStatusCode)
    {
        CompanyQuote = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
    }

请检查内容是否为空:

代码语言:javascript
运行
复制
    if (response.IsSuccessStatusCode && response.Content != null)
    {
        CompanyQuote = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55566869

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档