updev
updev

Reputation: 633

null reference exception while getting form value in MVC3

This is my code:

public ActionResult Register(FormCollection form)
{
    RegisterViewModel registerViewModel = new RegisterViewModel();
    registerViewModel.CaptchaInCache = new CacheWrapper().Get(form["GuidForCaptch"]).ToString();

    // some code here

}

I am getting null reference exception in the below line:

registerViewModel.CaptchaInCache =
    new CacheWrapper().Get(form["GuidForCaptch"]).ToString();

Can any one help me to resolve this. What am i doing wrong here.

Upvotes: 1

Views: 541

Answers (1)

Davin Tryon
Davin Tryon

Reputation: 67316

I think it might be too specific to your application to know exactly what to do. However, this should tell you exactly where the problem is:

var guidForCaptch = form["GuidForCaptch"];

if (guidForCaptch == null)
{
    throw new ArgumentNullException("GuidForCaptch is null");
}

var cacheWrapper = new CacheWrapper().Get(guidForCaptch);

if (cacheWrapper == null)
{
    throw new ArgumentNullException("CacheWrapper is null");
}

Upvotes: 2

Related Questions