对象引用未设置为对象的实例#3 [重复]

时间:2021-07-05 16:56:05

This question already has an answer here:

这个问题在这里已有答案:

I am getting the object reference error in just start of the method.

我刚刚在方法的开头得到了对象引用错误。

For Ex.:

259: public ActionResult ShowAddress(FormCollection formCollection)
260: {

In the above sample i am getting the error Line number 260.

在上面的示例中,我得到错误行号260。

2 个解决方案

#1


Here is the code from the question comments

这是问题评论中的代码

259: public ActionResult ShowAddress(FormCollection formCollection) { 
260:   long _userId= long.Parse(formCollection["UserId"].ToString()); 
261:   UserDetails _userDetails = _userDAL.GetUserDetails(_userId); 
262:   if(!string.IsNullOrEmpty(_userDetails.Address1)) return  RedirectToAction("GetAddress", "User"); else return View(); }

If you're seeing a NullReferenceException at line 260, either formCollection or the result of formCollection["UserId"] is null. You need to account for this in your code. For instance you could do the following.

如果您在第260行看到NullReferenceException,则formCollection或formCollection [“UserId”]的结果为null。您需要在代码中考虑到这一点。例如,您可以执行以下操作。

public ActionResult ShowAddress(FormCollection formCollection) {
  if ( null == formCollection ) { 
    return View();
  }
  object obj = formCollection["UserId"];
  if ( null == obj ) {  
    return View();
  }
  long _userId = long.Parse(obj.ToString());
  ...
}

#2


Finally, Enough information to attempt to post an answer...

最后,有足够的信息试图发布答案......

I suppose formCollection must be null.

我想formCollection必须为null。

PS: You'd benifit from reading this: http://catb.org/esr/faqs/smart-questions.html#intro Think of it as a life investment in life assurance.

PS:你读这篇文章是有益的:http://catb.org/esr/faqs/smart-questions.html#intro把它当作生命保障的生命投资。

#1


Here is the code from the question comments

这是问题评论中的代码

259: public ActionResult ShowAddress(FormCollection formCollection) { 
260:   long _userId= long.Parse(formCollection["UserId"].ToString()); 
261:   UserDetails _userDetails = _userDAL.GetUserDetails(_userId); 
262:   if(!string.IsNullOrEmpty(_userDetails.Address1)) return  RedirectToAction("GetAddress", "User"); else return View(); }

If you're seeing a NullReferenceException at line 260, either formCollection or the result of formCollection["UserId"] is null. You need to account for this in your code. For instance you could do the following.

如果您在第260行看到NullReferenceException,则formCollection或formCollection [“UserId”]的结果为null。您需要在代码中考虑到这一点。例如,您可以执行以下操作。

public ActionResult ShowAddress(FormCollection formCollection) {
  if ( null == formCollection ) { 
    return View();
  }
  object obj = formCollection["UserId"];
  if ( null == obj ) {  
    return View();
  }
  long _userId = long.Parse(obj.ToString());
  ...
}

#2


Finally, Enough information to attempt to post an answer...

最后,有足够的信息试图发布答案......

I suppose formCollection must be null.

我想formCollection必须为null。

PS: You'd benifit from reading this: http://catb.org/esr/faqs/smart-questions.html#intro Think of it as a life investment in life assurance.

PS:你读这篇文章是有益的:http://catb.org/esr/faqs/smart-questions.html#intro把它当作生命保障的生命投资。