我在方法刚开始时就遇到了对象引用错误。
例如:
259: public ActionResult ShowAddress(FormCollection formCollection)
260: {
在上面的示例中,我收到错误行号 260。
我在方法刚开始时就遇到了对象引用错误。
例如:
259: public ActionResult ShowAddress(FormCollection formCollection)
260: {
在上面的示例中,我收到错误行号 260。
这是问题评论中的代码
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(); }
如果您在第 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());
...
}
最后,有足够的信息来尝试发布答案......
我想 formCollection 必须为空。
PS:您将从阅读以下内容中受益:http : //catb.org/esr/faqs/smart-questions.html#intro 将其视为对人寿保险的终身投资。