Object reference not set to instance of object # 3

I get an object reference error only when the method starts.

Example:

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

In the above example, I get the error Line number 260.

+4
source share
2 answers

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 see a NullReferenceException on line 260, either formCollection or the result of formCollection ["UserId"] is null. You should consider this in your code. For example, you can do the following.

 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
source

Finally, enough information to try to send an answer ...

I suggest that formCollection should be null.

PS: You could read this: http://catb.org/esr/faqs/smart-questions.html#intro Think of it as living capital in providing life.

+1
source

All Articles