1

I'm new for entity framework. Now, I'm writing the delete function with a button. When I click this button, my data will be deleted; however, it will popup the alert that 'Object reference not set to an instance of an object.' I can't figure out this question.

private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (UserDataGrid.SelectedItems.Count == 0)
                    return;
                if (UserDataGrid.SelectedItems.Count > 0)
                    if (MessageBox.Show("Would you want to delete?", "Yes", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                    {
                        var selected = UserDataGrid.SelectedItem as User;
                        var index = selected.Index;

                        User user = new User();
                        user = ssEntities.User.Where(x => x.Index == index).First();

                        if (user != null)
                        {
                            ssEntities.User.DeleteObject(user);
                            ssEntities.SaveChanges();
                        }
                    }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
            finally { }
        }

If there is not enough information, plz tell me to add it.

12
  • 2
    what is the line that is throwing the exception? Commented Mar 6, 2013 at 11:53
  • 1
    better yet, do you see the "Would you want to delete?" message box before you get the exception? Commented Mar 6, 2013 at 11:55
  • w/o the line #, my guess would be either UserDataGrid is null or it's selected item is or is not convertible to User Commented Mar 6, 2013 at 11:56
  • It's so strange that my program did not go to the catch function. Commented Mar 6, 2013 at 11:56
  • 1
    In visual studio, it can be setup to stop on exception. Commented Mar 6, 2013 at 11:57

1 Answer 1

1

considering your comments, here is what you should do:

Since it looks like all your try-catch block are written as follows

try { }
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
finally { }

you have absolutely no idea where the exception is occurring, because you are losing the whole StackTrace. You should change this behavior, as there are a lot of problems with this. Find all the code-blocks which use this pattern and change it to something like this:

try { }
catch (Exception ex)
{ 
    Debug.WriteLine(string.Format("An exception has occurred in <some function>: {0}\n{1}", ex.Message, ex));
    MessageBox.Show(ex.Message); 
}
finally { }

This way, you will be able to see more detailed output in your Debug window, and you should be able to locate the exact exception by inspecting the StackTrace.
Off course, instead of using Debug.WriteLine, you could also use another logging mechanism like log4net.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your reply, but I have checked the breakpoint. It does not go to the catch block, and even not show up on the Debug window.
But have you changed all of the code blocks which use this try-catch block? The fact that this specific breakpoint never gets hit means that the exception occurs somewhere else.
Hey, @Roel Thanks for your advise. Now, I have known where I get wrong. I just have an excess column in my datagrid, so when I add it or delete it; it will get wrong.

Your Answer

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.