0

My models are:

public class Presentation
{
    public int PresentationId { get; set; }
    public string PresentationTitle { get; set; }
    public DateTime PresentationTime { get; set; }
    public Level? Level { get; set; }
    public virtual Professor Professor { get; set; }
}

public class Professor
{
    public int ProfessorId { get; set; }
    public string Name { get; set; }
    public DateTime EnrollmentDate { get; set; }
    public virtual ICollection<Presentation> Presentations { get; set; }
}

Level is an enum, forget about it for now. I have a web page where I try to add a new Presentation. This means I am adding a title,time,level and a professor. The professor is added through a DropDownList.

if (!IsPostBack)
{
     drpProfessor.DataSource = db.Professors.ToList();
     drpProfessor.DataTextField = "Name";
     drpProfessor.DataValueField = "ProfessorId";
     drpProfessor.DataBind();
}

The DropDownList is populated with the names, but on Save action I get a NullReference Exception. Save looks like this:

Presentation newPres = new Presentation();
newPres.PresentationTitle = txtPresentationTitle.Text;
newPres.Level = ParseEnum<Level>(drpLevel.SelectedValue);
newPres.PresentationTime = calendar.SelectedDate;
newPres.Professor.ProfessorId = Convert.ToInt32(drpProfessor.SelectedItem.Value);

db.Presentations.Add(newPres);
db.Entry(newPres).State = System.Data.EntityState.Added;

db.SaveChanges();
litMsg.Text = "New presentation added!";

Please ignore again the Level,it's working fine. But the error is at:

newPres.Professor.ProfessorId = Convert.ToInt32(drpProfessor.SelectedItem.Value);

I am trying to add the value (ProfessorId) of the selected item to db, but I get:

Object reference not set to an instance of an object.

I checked and the value(ProfessorId) is selected from the DropDownList.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
user3399858
  • 103
  • 1
  • 7
  • 2
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – juharr Sep 20 '15 at 17:35
  • most likely newPres.Professor is NULL. – DevilSuichiro Sep 20 '15 at 17:43
  • View it in the debugger and it will tell you exactly which variable is null. We can only guess. – Gert Arnold Sep 20 '15 at 18:07
  • From the initialization of `Presentation`, I see no where `Professor` could be init first before its `ProfessorId` is set. Your best bet is include the foreign key in the class `Presentation` and set the `ProfessorId` directly without having to access via `Professor`. – Hopeless Sep 20 '15 at 18:07
  • Try drpProfessor.SelectedValue instead of drpProfessor.SelectedItem.Value. I'm not sure... May be it helps... – Sankar Sep 20 '15 at 18:15
  • @SankarRaj I think it's the same, maybe remove `Value`, just keep `SelectedItem`, the Value is already set to `ProfessorId` part. `SelectedItem` would be `Professor`. – Hopeless Sep 20 '15 at 18:18
  • @Hopeless it might be same... But since selected item is a dropdownlist item, How can we assign it to the property or method? – Sankar Sep 20 '15 at 18:30
  • @SankarRaj I'm not sure about ASP.NET, in WPF it should be a `Professor`. If so we have no way to get access to the actual item (which is a `Professor`). As you see he already set `drpProfessor.DataValueField = "ProfessorId"` - that means `SelectedValue` should be `ProfessorId` and if `SelectedItem` is just a wrapper dropdownlist item, its `Value` should be the same as `SelectedValue`. – Hopeless Sep 20 '15 at 18:33
  • The value is selected,and the id is returned, but what I don't get is what's wrong with the declaration of Professor, or the relationship is not right? I know what can cause the exception,but I don't know how else to try to fix this. – user3399858 Sep 20 '15 at 19:09
  • @Hopeless - You are right, Professor is null, but how I should use ProfessorId directly from Presentation? – user3399858 Sep 21 '15 at 18:33
  • @user3399858 as I said, if possible you should add a foreign property `ProfessorId` to the `Presentation` class, then instead of setting `Professor`, use `ProfessorId`. If you cannot do that, you need to fetch the `Professor` from its Id, e.g: `newPres.Professor = db.Professor.Find(Convert.ToInt32(drpProfessor.SelectedItem.Value));` – Hopeless Sep 22 '15 at 00:14

0 Answers0