Click here to Skip to main content
15,915,509 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi i get error "object reference not set to an instance of an object"

I put this code insdie my Customerframe class

C#
private void tbFirstName_TextChanged(object sender, EventArgs e)
{
    if (tbFirstName.Text != string.Empty)
    {
        contact.FirstName = tbFirstName.Text;  //here is the error
         }
    else
        MessageBox.Show("You must fill in your name");
         tbFirstName.Focus();
}



and i get the error..

i try to make it reference to my contact class which has the property:

C#
class Contact
    {
        private Address adress;
        private Email email;
        private Phone phone;
        private string firstName;
        private string lastName;
        private string fullName;
  
public string FirstName
        {
            get
            {
                return firstName;
            }
            set
            {
                firstName = value;
            }
        }
   }



does anyone know my problem and how to make a solution?
Posted
Updated 25-Nov-11 3:54am
v3

Hi Goran Shoan,

You need to instantiate it first to make a new instance of that class so that you may use it.

C#
private void tbFirstName_TextChanged(object sender, EventArgs e)
{
    Contact contact = new Contact();
    if (tbFirstName.Text != string.Empty)
    {
        contact.FirstName = tbFirstName.Text;  //here is the error
    }
    else
        MessageBox.Show("You must fill in your name");
         tbFirstName.Focus();
    }
}
public string FirstName
        {
            get
            {
                return firstName;
            }
            set
            {
                firstName = value;
            }
        }


Best regards,
Eduard
 
Share this answer
 
Comments
Goran Shoan 26-Nov-11 8:39am    
but when doing that i get error CustomerRegister1.CustomerFiles.Contact does not contain a constructor that takes '0' arguments
Goran Shoan 26-Nov-11 8:43am    
I solved it :)
[no name] 26-Nov-11 20:58pm    
great!
Have you created an instance of your contact class?

I take it contact is a 'form level' variable (declared for the scope of your entire form). You need to initialize it during your form init\load

e.g.

private Contact contact;

public MyForm()
{
    contact = new Contact();
}
 
Share this answer
 
Contact is a class, so like any other class it needs an instance in order to work out exactly where to put it.
If you were talking about cars, it would be clearer: your code is saying:
"Put these gloves in the cars glove box."

Unless you specify which car you are talking about (my car, your car, the brown car over there) you can't put anything in it's glovebox.
You need to refer to an instance of Contact:
C#
Contact myContact = new Contact();
...

myContact.FirstName = tbFirstName.Text;


There are ways to make the compiler put data in a location which is available to all Contacts, but you definitely don't need that at the moment!
 
Share this answer
 
This error comes because of Object of class.Create a new object of the class to use the functionalities of that class.
C#
private Contact contact = new Contact();
//Creating New Object of Class Contact
private void tbFirstName_TextChanged(object sender, EventArgs e)
{
    if (tbFirstName.Text != string.Empty)
      {
        contact.FirstName = tbFirstName.Text;  //here is the error
      }
    else
      {
        MessageBox.Show("You must fill in your name");
        tbFirstName.Focus();
      }
}
 
Share this answer
 
In all cases of any exceptions, always try to run things under debugger. It will help you to find a problem yourself. Only if you still cannot resolve it, ask a question, but provide full exception information, indicate the affected lines of code in the code sample.

You can also catch all exceptions on top of stack (of each thread) and write a full exception dump. To get line numbers, do it in Debug configuration and write Exception.Stack. Don't forget to show all instances of Exception.InnerException, recursively.

—SA
 
Share this answer
 

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