C# - Object reference not defined for an object instance

Asked

Viewed 1,812 times

0

I have this mistake in my code:

 try
        {
            localhost.Agendamento a = new localhost.Agendamento();

            a.Cliente.Cpf = "1909009921";
            a.Servico.Cod_serv = 1;
            a.Data = Convert.ToDateTime("11/02/2017");
            a.Hora = Convert.ToDateTime("11:50");

            Service1 sv = new Service1();
            sv.CadastrarAgendamento(a);
            MessageBox.Show("Cadastrou!");
        }catch(Exception ex)
        {
            MessageBox.Show("Error" + ex.Message);
        }

The Exception activates when it reaches the line

a.Cliente.Cpf = "1909009921";

The exception is "Object reference not defined for an instance of an object"

  • Its object Cliente was instated ? could be the problem he is trying to access Cliente.cpf while Cliente is null. try debugging and check this.

3 answers

0


You need to create a client object in a.

a.Cliente = new CLiente();

0

What may be happening and that at the time your code tries to access a.Cliente.Cpf he finds his Cliente null, thus generating this exception, then you would have to instate your object Cliente.

Do the following:

a.Cliente = new CLiente();

0

Thank you to everyone who helped I managed was just instantiating a customer object and a service object within Scheduling

a.Cliente = new CLiente();
a.Servico = new Servico();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.