I have the ChamadaEfetuada(MAluno) : bool
method that verifies that the call was made for a given student, this method is used in another method ChamadaEfetuada() : bool
that instead of checking if the call was made to a single student, it checks all students at once, and it only returns true
if the call has been made to all students.
But I'm getting an error in my ChamadaEfetuada() : bool
method with the following message:
Object reference not set to an instance of an object.
The error occurs on this line:
alunos[i].IdAluno = Convert.ToInt32(registro["id_aluno"]);
The compiler says that the problem is in my array alunos
.
Complete method code ChamadaEfetuada(MAluno) : bool
:
public bool ChamadaEfetuada(MAluno aluno)
{
string query = "SELECT id_aluno FROM Lista_presenca WHERE id_aluno = " + aluno.IdAluno + " AND data = '" + this.Hoje() + "'";
DadosConexao dados_conexao = new DadosConexao();
SQLiteConnection conexao = this.conexao.Conexao;
conexao.Open();
SQLiteCommand command = conexao.CreateCommand();
command.CommandText = query;
SQLiteDataReader registro = command.ExecuteReader();
bool existe_registro = registro.HasRows;
conexao.Close();
return existe_registro;
}
Complete method code ChamadaEfetuada() : bool
:
public bool ChamadaEfetuada()
{
string query = "SELECT id_aluno FROM Alunos";
int total_alunos = 0, alunos_chamada_efetuada = 0;
DadosConexao dados_conexao = new DadosConexao();
SQLiteConnection conexao = this.conexao.Conexao;
conexao.Open();
SQLiteCommand command = conexao.CreateCommand();
command.CommandText = query;
SQLiteDataReader registro = command.ExecuteReader(); //Obtem os IDs de todos os alunos
MAluno[] alunos = new MAluno[registro.FieldCount];
total_alunos = registro.FieldCount;
int i = 0;
while (registro.Read())
{
alunos[i].IdAluno = Convert.ToInt32(registro["id_aluno"]); //Popula o atributo IdAluno do objeto alunos com os IDs obtidos pela query. Porem aqui acontece o erro.
i++;
}
conexao.Close();
for (i = 0; i < registro.FieldCount; i++)
{
if (this.ChamadaEfetuada(alunos[i]))
{
alunos_chamada_efetuada++;
}
}
if (alunos_chamada_efetuada == total_alunos)
return true;
else
return false;
}
The above two methods are part of my BLLListaPresenca
class.