Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, I am getting the error object reference is not set to a instance
I am creating a new customer in the combobox select method but in the button click I go to update the customer but I get an error where I am updating the customer object. The error is here in this btn click:

this exact code error:
Customer newCreatedCustomer = new Customer(currentUser.UserID, TextboxFirstName.Text,
TextboxLastName.Text, TextboxAddress.Text, TextboxCity.Text,
ComboboxState.SelectedValue.ToString(), TextboxZip.Text, TextEmailAddress.Text);
UsersDB.UpdateCustomer(newCreatedCustomer);



C#
private void BtnProfileEdit_Click(object sender, RoutedEventArgs e)
        {

            
            EnableControls();
            
            if (boolBtnPush == true)
            {
                if (BtnProfileEdit.Content.Equals("Save"))                
                {
                    if(boolIsCustomer == true)                    
                    {
                        if (TextboxFirstName.Text.Equals(""))
                        {   
                            MessageBox.Show("You 'MUST' enter a First name,", "WARNING", MessageBoxButton.OK);
                        }
                        else
                        {
                            try
                            {                                
                                Customer newCreatedCustomer = new Customer(currentUser.UserID, TextboxFirstName.Text,
                                    TextboxLastName.Text, TextboxAddress.Text, TextboxCity.Text,
                                    ComboboxState.SelectedValue.ToString(), TextboxZip.Text, TextEmailAddress.Text);
                                UsersDB.UpdateCustomer(newCreatedCustomer);

                                
                            }
                            catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
                        }
                    }
                    else
                    {
                        MessageBox.Show("You must select 'Yes' in combobox Customer\n" +
                            "to add customer data.", "UnAcceptable", MessageBoxButton.OK);
                    }                  
                }
            }
            //ON THE FIRST BUTTON CLICK DO THIS SO ON SECOND ABOVE CODE IS EXECUTED.
            boolBtnPush = true;
            BtnProfileEdit.Content = "Save";
        }

       
        private void CboCustomer_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string cboValue = "";
            if (CboCustomer.SelectedIndex > 0)
                cboValue = ((ComboBoxItem)CboCustomer.SelectedItem).Content.ToString();
                
            if(cboValue.Equals("Yes"))          
            {
                boolIsCustomer = true;
                User addCustomer = null;
                Customer newCustomer = null;

                MessageBoxResult result = MessageBox.Show("Updating database to customer status.",
                    "Customer Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Exclamation);
                if (result == MessageBoxResult.Yes)
                {
                    try
                    {
                        addCustomer = new User(currentUser.UserID, currentUser.Username,
                            currentUser.Password, currentUser.IsAdmin, currentUser.UserCreatedDate, boolIsCustomer);
                        UsersDB.UpdateCurrentUser(addCustomer);
                        //Create New Customer
                        newCustomer = new Customer(currentUser.UserID, currentUser.Username, null, null,
                                                    null, null, null, null);
                        UsersDB.CreateCustomer(newCustomer);
}
                    catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
                }
                //else
                   // boolIsCustomer = false;
            }
        }


What I have tried:

making new object different ways putting this code in other sections
Posted
Updated 11-Nov-18 21:30pm

1 solution

Hi I am more suspicious to the below line:
ComboboxState.SelectedValue.ToString()

Could you please do a favor and re-write your code in this way:

C#
var userId = currentUser.UserID;
var firstName = TextboxFirstName.Text;
var lastName = TextboxLastName.Text;
var address = TextboxAddress.Text;
var city = TextboxCity.Text;
var state = ComboboxState.SelectedValue.ToString();
var zip = TextboxZip.Text;
var email = TextEmailAddress.Text;

Customer newCreatedCustomer = new Customer(userId, firstName,
lastName, address,city,
state, zip, email);
UsersDB.UpdateCustomer(newCreatedCustomer);


This is more traceable because you can simply put a breakpoint at the first line and go step-by-step with F10 (if you are using MS-VS) and find out where is the problem. Somewhere you are running an action on a null object. For example: if the ComboboxState.SelectedValue is null then that line will throw null reference object error due to conversion to a string.

Cheers,
AH
 
Share this answer
 
v2
Comments
TheBigBearNow 12-Nov-18 15:59pm    
thank you! that is precisely what I needed to hear and see that is where my issues lies I will figure out the correct code to fix this!
Aydin Homay 12-Nov-18 16:00pm    
Happy to hear that, good luck

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900