4

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.

John Saunders
  • 157,405
  • 24
  • 229
  • 388
Prasad
  • 56,343
  • 61
  • 142
  • 199
  • When asking a language-specific question, please specify the language you are asking about. –  May 23 '09 at 07:40
  • @Prasad: Do you believe that everyone works on the very same programming language you do? The same framework or platform? The same versions? – Cerebrus May 23 '09 at 07:43
  • oops. i forgot to specify that. The Code is from the MVC Controller. – Prasad May 23 '09 at 07:45
  • YYou still haven't specified a language! MVC is a pattern used by many languages –  May 23 '09 at 07:46
  • sorry again, its MVC with C#. i am getong this error while the post action. – Prasad May 23 '09 at 07:48
  • often when you get an error on a line with nothing but a curly brace or the like it means the problem is in one of the preceding lines (and is often a missing curly brace or the like, above). You'll need to show more relevant code to help diagnose this. – jess May 23 '09 at 08:09
  • Here is the complete code of the method that i get exception sometimes: [AcceptVerbs(HttpVerbs.Post)] public ActionResult ShowAddress(FormCollection formCollection) { long _userId= long.Parse(formCollection["UserId"].ToString()); UserDetails _userDetails = _userDAL.GetUserDetails(_userId); if(!string.IsNullOrEmpty(_userDetails.Address1)) return RedirectToAction("GetAddress", "User"); else return View(); } – Prasad May 23 '09 at 09:14
  • I'm not sure why readers get indignant about poor questions or missing information. It hurts the OP more than anyone. Either help the OP out or don't. – shannon May 18 '13 at 21:33

2 Answers2

2

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.

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());
  ...
}
JaredPar
  • 673,544
  • 139
  • 1,186
  • 1,421
  • Consider, 259: public ActionResult ShowAddress(FormCollection formCollection) 260: { 261: long _userId= long.Parse(formCollection["UserId"].ToString()); i am getting the error in Line 260, not while accessing the formcollection. – Prasad May 23 '09 at 10:34
  • @Prasad, why do you think it's line 260? Are you getting the line number from the exception information? If so it's possible that the exception and editor have different line numbers because one starts at 0 and the other 1. Also, have you attached a debugger to see what is actually null here? – JaredPar May 23 '09 at 10:37
  • please see the below stack trace. copying partial as i am not allowed to enter more than 600 chars. Stack Trace : at MVCApp.Controllers.UserController.ShowAddress(FormCollection formCollection) in C:\My Projects\MVCApp\Controllers\UserController.cs:line 260 at lambda_method(ExecutionScope , ControllerBase , Object[] ) at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) – Prasad May 23 '09 at 10:41
1

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

I suppose formCollection must be 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.

corlettk
  • 12,040
  • 7
  • 35
  • 50