This simple code:
[HttpPost]
public ActionResult Check(string chosen_username, string email) {
var lang = RouteData.Values["lang"]; ViewBag.Language = lang;
string un; string em;
try {
un = chosen_username; em = email;
}
catch { un = ""; em = ""; }
string s = un.Length.ToString();
ViewBag.test = s;
return View();
}
Throws the object reference not set to an instance of an object-exception (on the line that reads “string s = un.Length.ToString();”
Could anybody help a C# newbie out please?
I think you did not initialize ViewBag object or it is null.
Try this
To be sure that problem is not in string (and it’s not, believe me) try this
and use debugger to check that s is the one you expected.
EDITED after OP posted new code:
Simply: string un is null!!
Try using