1
I’m having a problem in C#, I’m facing an exception and I can’t recognize why
Details of the Exception:
System.Nullreferenceexception: "Object reference not defined for an instance of an object."
Code snippet that throws the exception is:
public void AdicionarCliente(String nome, String sobrenome)
{
Clientes.Add(new Cliente(nome, sobrenome));
}
In class:
public sealed class Banco
{
private static readonly Banco instance = new Banco();
public static Banco Instance
{
get
{
return instance;
}
}
private List<Cliente> Clientes;
private Banco()
{
}
public void AdicionarCliente(String nome, String sobrenome)
{
Clientes.Add(new Cliente(nome, sobrenome));
}
public int GetNumeroDeClientes()
{
return Clientes.Count;
}
public Cliente GetCliente(int indice)
{
return Clientes[indice];
}
}
Client class:
public class Cliente
{
public String Nome
{
get;
private set;
}
public String Sobrenome
{
get;
private set;
}
private List<Conta> Contas { set; get; }
public Cliente(String nome, String sobrenome)
{
this.Nome = nome;
this.Sobrenome = sobrenome;
}
public Conta GetConta(int indice)
{
return Contas[indice];
}
public int GetNumeroDeContas()
{
return Contas.Count;
}
public void AdicionarConta(Conta c)
{
Contas.Add(c);
}
}