0

I have a code with two table adapters (Agentes_QuimicosTableAdapter and General_Information_ControlTableAdapter). The table adapters were brought by the DataSet according this image 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?

  • A NullReference typically means that what you are trying to use, is null, and the action you are taking requires the object to have a value. It is not always enough to do "new object();". This will initialize it yes, but it wont give it a value per se. So try to debug and find out why it is null and why you think that it shouldn't be null. Maybe you will find your answer there. However, i have not worked with "TableAdapterManager" and couldn't find out in what context it is used, therefor this is a comment and not an answer. – RatzMouze Nov 04 '22 at 12:58

0 Answers0