1

I'm trying to delete a row from a table in my database but I keep getting a null reference exception.

I know that the code I wrote isn't grabbing the ID number from the ComboBox but I don't know how to fix it.

Here's the code I have:

private void btnDelete_Click(object sender, RoutedEventArgs e)
{
 try
 {
 //select row to delete
 Doctor del = ((Doctor)cbDocIdd.SelectedItem);
 Doctor deleted = (from d in MainWindow.nlh.Doctors
 where d.DoctorID == del.DoctorID
 select d).First();
 //delete row from db
 MainWindow.nlh.Doctors.DeleteObject(deleted);
 //Save to database
 MainWindow.nlh.SaveChanges();
 MessageBox.Show("Doctor deleted");
 this.Close();
 }
 catch (Exception ex)
 {
 MessageBox.Show(ex.Message);
 }
}

Any help would be greatly appreciated.

This is the code used to fill the combobox:

private void Window_Loaded(object sender, RoutedEventArgs e)
 {
 cbDocIdd.DataContext = MainWindow.nlh.Doctors;
 }
asked Jun 13, 2015 at 13:14
3
  • 1
    Are you sure the doctor with this id exist. Commented Jun 13, 2015 at 13:19
  • yes. the combobox is populated from the database. Commented Jun 13, 2015 at 14:19
  • So where does this NRE occur? Commented Jun 13, 2015 at 19:39

2 Answers 2

2

You need to use .FirstOrDefault and check whether a valid entity has been found:

//select row to delete
Doctor del = ((Doctor)cbDocIdd.SelectedItem);
Doctor deleted = (from d in MainWindow.nlh.Doctors
 where d.DoctorID == del.DoctorID
 select d).FirstOrDefault();
// check if it even exists!!
if(deleted != null) 
{ 
 //delete row from db
 MainWindow.nlh.Doctors.DeleteObject(deleted);
 //Save to database
 MainWindow.nlh.SaveChanges();
 MessageBox.Show("Doctor deleted");
 this.Close();
}

If you use .First() and the doctor with that given ID doesn't exist, you'll get an exception

Also: make sure the del value is OK and not null before using it in the following statement. Same goes for MainWindow - are you sure this is not null ??

Update: can you try these two lines and tell me what the result is??

//select row to delete
object selectedObj = cbDocIdd.SelectedItem;
if(selectedObj != null)
{
 string typeOfSelectedObj = selectedObj.GetType().Name;
}
Doctor del = ((Doctor)cbDocIdd.SelectedItem);

Is selectedObj something other than null?? And if so: what type is it??

Update #2: Ok - so the doctor does exist and is being returned OK - can you try this for me??

Replace this line:

Doctor del = ((Doctor)cbDocIdd.SelectedItem);

with this instead:

Doctor del = cbDocIdd.SelectedItem as Doctor;

When you run this - is del now something other than null ?

Solution:

In the end, the real reason why this code broke lies in the fact that the call to MainWindow.nlh.Doctors.DeleteObject(deleted); caused an event handler to fire, which included this code:

private void cbDocIdd_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
 // populate with DoctorID from db
 Doctor deletedDoc = ((Doctor)cbDocIdd.SelectedItem);
 tbLastName.Text = deletedDoc.LastName.ToString();
 tbFirstName.Text = deletedDoc.FirstName.ToString();
 tbLicenseNumber.Text = deletedDoc.LicenseNumber.ToString();
}

but in this situation, when a doctor is being deleted, the deletedDoc was returned as NULL, but no check is in place to ensure to access only a non-NULL object ..... therefore, an infamous "null-reference exception" was thrown when trying to access the deletedDoc.LastName property...

answered Jun 13, 2015 at 13:30
Sign up to request clarification or add additional context in comments.

6 Comments

I've tried it with .FirstOrDefault() and it's not helping because the ID does exist (this is a school project and the combobox is populated straight from the db, with only valid IDs). del comes back null but i can't figure out what I'm doing wrong.
This is how it's filled:
private void cbDocIdd_SelectionChanged(object sender, SelectionChangedEventArgs e) { //populate with DoctorID from db Doctor deletedDoc = ((Doctor)cbDocIdd.SelectedItem); tbLastName.Text = deletedDoc.LastName.ToString(); tbFirstName.Text = deletedDoc.FirstName.ToString(); tbLicenseNumber.Text = deletedDoc.LicenseNumber.ToString(); }
Ok if I'm reading this right, selectedobj shows the right doctor but the type says null. Not sure if that makes sense, as i said, i'm very new at this.
Boy, if only we (or better: the OP) would have known the line that threw the exception! You're a terrier Marc!
|
0

"null reference exception" shows when you try to use a method that is containing null value.

Such as if product have null value or assigned to null like product=null then you cannot use method as product.count() or something because null.count() is not supported.

 Doctor deleted = (from d in MainWindow.nlh.Doctors
 where d.DoctorID == del.DoctorID
 select d).FirstOrDefault(); 
use this and check null.
marc_s
760k186 gold badges1.4k silver badges1.5k bronze badges
answered Jun 13, 2015 at 14:36

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.