C # - Object reference not set to an instance of an object [closed]

0

I have this error 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);
        }

O the exception active when it arrives on the line

a.Cliente.Cpf = "1909009921";

The exception that gives is "Object reference not set to an instance of an object"

    
asked by anonymous 01.06.2017 / 00:38

3 answers

0

You need to create a client object in a.

a.Cliente = new CLiente();
    
01.06.2017 / 00:43
0

What could be happening and that at the time your code tries to access a.Cliente.Cpf it finds its Cliente null, thus generating this exception, then you would have to instantiate its Cliente object.

Do the following:

a.Cliente = new CLiente();
    
01.06.2017 / 00:47
0

Thanks to all who helped I managed to just instantiate a client object and a service object within Schedule

a.Cliente = new CLiente();
a.Servico = new Servico();
    
01.06.2017 / 00:57