views:

29

answers:

1

Hello I have the following code and i don't know were is the mistake

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(factura fac)
    {
        if (fac.numar>0)
            ModelState.AddModelError("numar", "Numar este invalid .");
        if (fac.serie.Trim().Length == 0)
            ModelState.AddModelError("serie", "Serie invalida");

        if (!ModelState.IsValid) return View("Create", fac);

    }

Here i try validate an texbox "serie" and i got the following error Object reference not set to an instance of an object.

thank you

+1  A: 

First, could you please re-format to make easier to read? ie put all the code in a block?

Next, debug and check these expressions to see if they're null:

  • fac.serie
  • fac

It looks like either being null could throw this exception. It's probably the latter. If appropriate, wrap in a guard condition to check if it is null before evaluating.

Bobby