I have a code with two table adapters (Agentes_QuimicosTableAdapter and General_Information_ControlTableAdapter). The table adapters were brought by the DataSet according and then were used on code:
private float riskIndexBySubstanceCalculation()
{
tableAdapterManager = new TableAdapterManager();
tableAdapterManager.Agentes_QuimicosTableAdapter = new Agentes_QuimicosTableAdapter();
tableAdapterManager.General_Information_ControlTableAdapter = new General_Information_ControlTableAdapter();
// Gerar lista de agentes para calcular o tamanho do array a ser usado em "listOfFinalAgents"
var agentList = tableAdapterManager.Agentes_QuimicosTableAdapter.GetData()
.Select(q => new
{
Id = q.id,
Nome = q.nome,
CAS = q.cas,
Quantidade = q.quantidade,
Unidade = q.unidade_medida,
MR = q.massa_referencia
})
.ToList();
Utils.Agents[] listOfFinalAgents = new Utils.Agents[agentList.Count];
// Criar uma nova lista de agentes contendo o parâmentro "mla" e preenchê-la
for (int i = 0; i < agentList.Count; i++)
{
listOfFinalAgents[i] = new Utils.Agents(agentList[i].Nome, agentList[i].Quantidade,
agentList[i].Unidade, agentList[i].MR, agentList[i].MR * (float)0.2, 0);
}
// Recuperar as informações gerais e, posteriormente, recuperar a "distância"
var sourceTable = tableAdapterManager.General_Information_ControlTableAdapter.GetData();
var source = sourceTable.FirstOrDefault(q =>
q.nome_responsavel == this.generalInformationControl.responsiblePersonName.Text);
int distance = source.distancia;
// Calcular o FD e o Índice de Risco de cada agente
float FD = distance / 50;
foreach (var agent in listOfFinalAgents)
{
agent.Indice_Risco = (agent.MLA / agent.MR) / FD;
}
this.agentes_QuimicosDataGridView.DataSource = listOfFinalAgents;
return 2;
}
I do not know why, but only the second table adapter is causing a null reference problem on the line of var "sourceTable".
I have instantiate the two table adapters: "tableAdapterManager.Agentes_QuimicosTableAdapter" and "tableAdapterManager.General_Information_ControlTableAdapter", but with de second, on line of var "sourceTable" is occur this problem: "Object reference not set to an instance of an object".
Why this error is having occur?