【问题标题】:NullReferenceException when setting string wp7设置字符串 wp7 时出现 NullReferenceException
【发布时间】:2011-08-03 13:34:27
【问题描述】:
var response = JObject.Parse(e);
                    foreach (var item in _wall) {
                        var yh = from ix in response["response"]
                                 where (int)ix["uid"] == item.from_id
                                 select ix;
                        if (yh != null) {
                            item.image_uri = yh.FirstOrDefault()["photo_medium_rec"].ToString(); //EXCEPTION
                            item.author_name = yh.FirstOrDefault()["first_name"].ToString() + " " + yh.FirstOrDefault()["last_name"].ToString();
                        }
                    }

前两项也不例外,但第三项是: 堆栈跟踪:

 at iVk.UserPage.get_profiles_info(String e)
   at iVk.App.<>c__DisplayClass2.<>c__DisplayClass4.<get_data>b__1()
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at System.DelegateПервый этап обработки исключения типа "System.Exception" в приложении System.Windows.dll

Json 数据:

{
  "response": [
    {
      "uid": 92026953,
      "first_name": "Kristina",
      "last_name": "Valkonskaya",
      "photo_medium_rec": "http://cs11163.vkontakte.ru/u92026953/d_2de22683.jpg"
    },
    {
      "uid": 36798445,
      "first_name": "Марина",
      "last_name": "Соболевская",
      "photo_medium_rec": "http://cs10575.vkontakte.ru/u36798445/d_c70884c3.jpg"
    }
  ]
}

【问题讨论】:

    标签: c# string windows-phone-7 nullreferenceexception


    【解决方案1】:

    你的 linq 查询总是会返回一个枚举,即使是空的。

    也许:

      var response = JObject.Parse(e);
      foreach (var item in _wall) {
          var yh = (from ix in response["response"]
                    where (int)ix["uid"] == item.from_id
                    select ix).FirstOrDefault();
          if (yh != null) {
              item.image_uri = yh["photo_medium_rec"].ToString();
              item.author_name = yh["first_name"] + " " + yh["last_name"];
          }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多