-1

I am building a WCF application, with multiple layers, and I am given this exception while trying to add new object to the DB. I will provide you code samples, please try to help me out here. Error is on the line scenaristBLL.Insert(s);

Here is my method within which I am trying to create a new Scenarist object in ScenaristController class:

[HttpPost]
        public ActionResult Create(ScenaristViewModel fvm, List<int> scenarijId)
        {
            if (fvm != null)
            {
                if (String.IsNullOrEmpty(fvm.Scenarist.NameSurname) == true)
                {
                    TempData["ValidationErrorIme"] = "validation err1";
                    return RedirectToAction("Create");
                }
                if (fvm.Scenarist.DateOfBirth == null)
                {
                    TempData["ValidationErrorGodina"] = "validation err2";
                    return RedirectToAction("Create");
                }

                Scenarist s = new Scenarist();
                s.NameSurname = fvm.Scenarist.NameSurname;
                s.DateOfBirth = fvm.Scenarist.DateOfBirth;
                s.dateOfDeath = fvm.Scenarist.dateOfDeath;
                scenaristBLL.Insert(s); // PROBLEM!
                s = scenaristBLL.Fetch(s.Id);
                foreach (int scenarij in scenarijId.Where(z => z != 0))
                {
                    Scenarij scn = scenarijBLL.Fetch(scenarij);
                    if (scn == null)
                    {
                        return new HttpNotFoundResult("Nepostojeći scenaristId: " + scenarij.ToString());
                    }
                    scenarijBLL.AddToScenarist(scn, s);
                }
            }
            return RedirectToAction("Index");
        }

Also, here is the View class for it:

<table>
            <tr>
                <td>Name and surname:</td>
                <td>@Html.TextBoxFor(modelItem => Model.Scenarist.NameSurname) @TempData["ValidationError1"]</td>
            </tr>
            <tr>
                <td>Date of birth:</td>
                <td>@Html.TextBoxFor(modelItem => Model.Scenarist.DateOfBirth) @TempData["ValidationError2"]</td>
            </tr>
            <tr>
                <td>Datum of Death:</td>
                <td>@Html.TextBoxFor(modelItem => Model.Scenarist.DateOfDeath) @TempData["ValidationError3"]</td>
            </tr>
        </table>

I think the DateOfBirth and DateOfDeath might be the problem, since those are in DateTime format, and I am not sure how to get a DateTime format from user input. Just a guess, really not sure.

Furtherly, here are other layers for it... BLL:

public void Insert(Scenarist scenarist)
{
    string error;
    if (scenarist.Validate(out error) == true)
    {
        string[] nameSurname = scenarist.NameSurname.Split(' ');
        string name = nameSurname[0];
        string surname = nameSurname[1];
        dal.Insert(name, null, surname, scenarist.DateOfBirth, scenarist.DateOfDeath);
    }
    else
    {
        throw new ArgumentException("Validation error: " + error);
    }
}

DAL:

public void Insert(string name, string middleName, string surname, 
    DateTime? dateOfB, DateTime? dateOfD)
{
    using (RPPP111Entities ctx = new RPPP111Entities())
    {
        Scenarist scenarist = new Scenarist();
        scenarist.Person.Name = name;
        scenarist.Person.Middlename = middleName;
        scenarist.Person.surname = surname;
        scenarist.Person.DateOfB = dateOfB;
        scenarist.Person.DateOfD = dateOfD;
        ctx.Scenarist.Add(scenarist);
        ctx.SaveChanges();
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Whizzil
  • 1,264
  • 6
  • 22
  • 39
  • Try to step through it with the Visual Studio debugger to see what the null object is. Also, why do you have == true in an If statement? By definition of an If statement, you don't have to do that. – mason Jun 24 '13 at 23:25
  • 3
    When you debug and set a breakpoint on your problem line of code, is scenaristBLL set to null? – Matt Johnson Jun 24 '13 at 23:26
  • I am brand new to VS2012, not really sure how to use it's debugger yet. I will try however. Any ideas on the problem? – Whizzil Jun 24 '13 at 23:34

2 Answers2

1

Probably scenarist.NameSurname (Insert function) is null, because in your View you've got modelItem => Model.Scenarist.NameSurnameScenarist.

EDIT:
Debbuging tutorial: http://www.youtube.com/watch?v=C0vDKXIq_9A

EDIT2:
Have you created instance of Person in (Insert DAL function):

scenarist.Person.Name
Wojciech Kulik
  • 7,823
  • 6
  • 41
  • 67
  • Actually, it was just a bad translation, no it's the same attribute. I will edit it now, so that it is consistent. – Whizzil Jun 24 '13 at 23:41
  • 1
    @Whizzil ok, it's hard to say where is a null, you must to debug code and find it by yourself. Watch the video ;-). – Wojciech Kulik Jun 24 '13 at 23:43
0

If the problem truly happens when you hit scenaristBLL.Insert(s); // PROBLEM!, then the only way this can happen is if scenaristBLL is null. Perhaps Insert should be a static function and scenaristBLL should be an abstract class.

You can confirm this by running and then when you hit the error, hover your mouse over scenaristBLL. It will say if it's null.

Thanks to @Matt Johnson for noticing it.

mason
  • 31,774
  • 10
  • 77
  • 121