In my application I have a chained list class that receives a generic parameter, which will be the type of value that the list will save. In this case, which I am passing as a parameter to the Usuario
class, I need to check if there already exists a Usuario
registered with the registered CPF. For this, I created the Procura
method, to which I inform the value that I want to compare and the attribute that 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 resolve 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;
...
}