1
In my application I have a chained list class that takes a generic parameter, which is the type of value the list will store. In this case, I am passing as parameter the class Usuario
, I need to check if there is already a Usuario
registered with the informed CPF. For this, I created the method Procura
, to which I inform the value I wish to compare and the attribute I need to access, but the following error is occurring:
System.Nullreferenceexception: 'Object reference not set to an instance of an object.'
System.Type.Getproperty(...) returned null.
Apparently, GetProperty()
is returning null. How can I solve this problem?
public class ListaEncadeada<T>{
...
public Boolean Procura(String valor, String atributo)
{
var atual = cabeca;
while (atual != null)
{
var obj = atual.Valor;
String aux = obj.GetType().GetProperty(atributo).GetValue(obj).ToString();
if (aux == valor)
{
return true;
}
atual = atual.Proximo;
}
return false;
}
}
.
private void Form1_Load(object sender, EventArgs e)
{
...
//verificar se já existe um usuário com o cpf informado.
listaUsuario.Procura(valor, "cpf");
}
.
public class Usuario
{
private String nome;
private String cpf;
private String senha;
...
}
The estate
cpf
exists under the same name (attention to case sensitive) in the objectobj
?– João Martins
@Joãomartins, exists in the User class, to which the object belongs.
– user75204
Do
debug
and check whether the objectobj
is correctly filled, of what type is and if you have the properties you want.– João Martins
@Joãomartins, yes, I did the debug and the object is correctly filled, but using the Getproperties() method nothing is returned.
– user75204