Inclusion of data/ object

Asked

Viewed 22 times

0

I am having a problem in including objects from other classes. What would be the best solution for this?

For example, I have a Service class and attributes that are other classes, such as Customer and Vehicle. As listed below:

public class Servico
    { 
        public int Id { get; set; }
        public string Nome { get; set; }
        public string Descricao { get; set; }
        public Cliente cliente { get; set; }
        public Veiculo  veiculo{ get; set; }
        public double Valor { get; set; } 
        public DateTime DataServico { get; set; }
        
        public Servico()
       {
        new Cliente(),
        new Veiculo()
        }
   
    }

Plus I have the method inside the controller that will include the data and receive the objects. As I demonstrate just below...

[HttpPost]
        [Route("Incluir")]
        public HttpResponseMessage Incluir(Servico servico)
        {
            try
            {
                bool res = false;

                if (servico == null)
                {
                    throw new ArgumentNullException("servico");
                }

                Conexao cx = new Conexao();
                cx.ConectarBase();
                SqlCommand command = new SqlCommand();
                command.Connection = cx.connection;
                command.CommandText = "EXEC INCLUIR_SERVICO @NOME, @DESCRICAO, @CLIENTE, @VEICULO, @VALOR";

                command.Parameters.AddWithValue("nome", servico.Nome);
                command.Parameters.AddWithValue("descricao", ((object)servico.Descricao) ?? DBNull.Value);
                command.Parameters.AddWithValue("cliente", ((object)servico.veiculo.Id) ?? DBNull.Value);
                command.Parameters.AddWithValue("veiculo", ((object)servico.cliente.Id) ?? DBNull.Value);
                command.Parameters.AddWithValue("valor", ((object)servico.Valor) ?? DBNull.Value);

                int i = command.ExecuteNonQuery();
                res = i > 0;

                cx.DesconectarBase();

                return Request.CreateResponse(HttpStatusCode.OK, res);
            }
            catch (Exception ex)
            {

                return Request.CreateResponse(HttpStatusCode.BadRequest, ex);
            }

        } 

However, in the objects I have null error Exception in the properties Servico.Cliente.Id and Servico.Veiculo.ID.

Please, can someone help me understand how I should resolve this issue?.

1 answer

0


The error of null Exception occurs when the instance of a null referenced object, i.e., does not reference any object position in memory. You got this because in the class builder Service, just instated class objects Client and Vehicle, but did not associate them with attributes Client and Vehicle, respectively. To resolve the error, it is necessary to associate the attributes to the instances:

public class Servico
{ 
    public int Id { get; set; }
    public string Nome { get; set; }
    public string Descricao { get; set; }
    public Cliente cliente { get; set; }
    public Veiculo  veiculo{ get; set; }
    public double Valor { get; set; } 
    public DateTime DataServico { get; set; }
    
    public Servico()
   {
       cliente = new Cliente();
       veiculo = new Veiculo();
    }

}

Browser other questions tagged

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