Object error not instantiated

1

I have a model named ClientesModel . Inside it I have the fields:

    public int id { get; set; }
    public int codcli { get; set; }
    public string nome { get; set; }
    public string endereco { get; set; }
    public string nr { get; set; }
    public string bairro { get; set; }
    public string cidade { get; set; }

I also have a model state called with the following fields:

    public int id { get; set; }
    public string sigla { get; set; }

How would you link these two models ? I tried to do this:

    public int id { get; set; }
    public int codcli { get; set; }
    public string nome { get; set; }
    public string endereco { get; set; }
    public string nr { get; set; }
    public string bairro { get; set; }
    public string cidade { get; set; }
    public EstadosModel estado { get; set; } 

And when putting the information in model , use this way:

    ClientesModel c = new ClientesModel();
    c.id = Convert.ToInt32(Dr["id"]);
    c.codcli = Convert.ToInt32(Dr["codcli"]);
    c.nome = Convert.ToString(Dr["nome"]);
    c.endereco = Convert.ToString(Dr["endereco"]);
    c.nr = Convert.ToString(Dr["nr"]);
    c.bairro = Convert.ToString(Dr["bairro"]);
    c.cidade = Convert.ToString(Dr["cidade"]);
    c.estado.id = 1;
    c.estado.sigla = Convert.ToString(Dr["sigla"]);

But it ends up with an error.

  

Object reference not set to an instance of an object.

I know that I need to instantiate the object but not without where or how to instantiate an object.

I do not know if I'm messing around either and should use the 2 separate models .

    
asked by anonymous 22.04.2016 / 21:17

2 answers

3

If specific to your problem, the c.estado object needs to be instantiated before you assign any value to the id and sigla properties.

You can do this in the method itself:

ClientesModel c = new ClientesModel();
c.id = Convert.ToInt32(Dr["id"]);
c.codcli = Convert.ToInt32(Dr["codcli"]);
c.nome = Convert.ToString(Dr["nome"]);
c.endereco = Convert.ToString(Dr["endereco"]);
c.nr = Convert.ToString(Dr["nr"]);
c.bairro = Convert.ToString(Dr["bairro"]);
c.cidade = Convert.ToString(Dr["cidade"]);
//Instanciando um novo estado e atribuindo sua referência a c.estado
c.estado = new EstadosModel();
c.estado.id = 1;
c.estado.sigla = Convert.ToString(Dr["sigla"]);

As well as putting a default constructor in the ClientsModel class:

public int id { get; set; }
public int codcli { get; set; }
public string nome { get; set; }
public string endereco { get; set; }
public string nr { get; set; }
public string bairro { get; set; }
public string cidade { get; set; }
public EstadosModel estado { get; set; } 
//Construtor padrão
public ClientesModel()
{
     this.estado = new EstadosModel();
}

Notes

  • The properties in c # follow the PascalCase nomenclature (ie first letter of each capitalized word, for example: CustomerCode)
  • Prefer a code that is more readable than a "leaner" code. There are no needs (at first glance, at least) for example to abbreviate the codcli property, or the numero property.
  • Whenever you create a new variable, try to make your name as explanatory as possible. In complex or extended code, this feature can be a very big differential for the correct understanding of the code without much effort.
  • When naming your classes, use the plural only when it makes sense. If your class represents only one client, it does not make sense to put your plural name (clients). Same for state and states. A good example for using the plural is in properties that are lists, which represent more than one object.

Following the above recommendations, your code would look something like this:

ClientModel.cs

public int Id { get; set; }
public int CodigoCliente { get; set; }
public string Nome { get; set; }
public string Endereco { get; set; }
public string Numero { get; set; }
public string Bairro { get; set; }
public string Cidade { get; set; }
public EstadoModel Estado { get; set; } 

StateModel.cs

public int Id { get; set; }
public string Sigla{ get; set; }

Main.cs

ClienteModel cliente = new ClienteModel();
cliente .id = Convert.ToInt32(Dr["id"]);
cliente.codcli = Convert.ToInt32(Dr["codcli"]);
cliente.nome = Convert.ToString(Dr["nome"]);
cliente.endereco = Convert.ToString(Dr["endereco"]);
c.nr = Convert.ToString(Dr["nr"]);
cliente.bairro = Convert.ToString(Dr["bairro"]);
cliente.cidade = Convert.ToString(Dr["cidade"]);
cliente.estado.id = 1;
cliente.estado.sigla = Convert.ToString(Dr["sigla"]);
    
22.04.2016 / 21:22
1

Vinicius's answer is perfect, at least as far as it goes over the code presented (I will not go into detail of what could be different that is not the focus of the question). I'll just show another way to do the initialization of the object if using C # 6 up:

public EstadosModel Estado { get; set; } = new EstadosModel();

So you do not have to have the builder, at least not for this.

Another possible improvement is to use initialization and object :

var cliente = new ClienteModel {
    id = Convert.ToInt32(Dr["id"]),
    codcli = Convert.ToInt32(Dr["codcli"]),
    nome = Dr["nome"],
    endereco = Dr["endereco"],
    nr = Dr["nr"],
    bairro =Dr["bairro"],
    cidade = Dr["cidade"],
    estado.id = 1,
    estado.sigla = Dr["sigla"]
};

As I imagine the data comes as string in this dictionary, you do not have to convert to string .

Note that if there is any reason for the data to be malformed, Convert.ToInt32() will generate an exception, this is not ideal.

Are you sure that the client code needs to be numeric?

And taking the time to make style notes, follow the C # style guide .

    
23.04.2016 / 00:24