Object reference not defined for an object instance - (objects are not null)

Asked

Viewed 607 times

0

Good afternoon,

I do not understand some concepts and I have great doubt regarding this problem, I did some research to try to identify, but without success.

I have this code and is returning error when calling the Log2 method

Parallel.ForEach(grupoServidores, new ParallelOptions { MaxDegreeOfParallelism = 80 },
servidor =>
{
    //Console.WriteLine((servidor);
    //Console.WriteLine((servidor);
    string mensagem = servidor;
    //Console.WriteLine((mensagem);

    // Variavel de timeout da thread
    var timeout = 30; // 22 seconds

    //Variavel de latencia da thread

    DateTime dataCheck = DateTime.Now;


    //Thread paralela com timeout
    Thread threadAgent = new Thread(() => AgentCheckThread(mensagem));
    threadAgent.IsBackground = true;
    threadAgent.Name = "Agent";
    threadAgent.Start();

    //Console.WriteLine(DateTime.Now + ":Estado da thread assim que inicia: " + threadAgent.ThreadState + " para o servidor: " + mensagem);
    Log(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + ";" + "Servidor=" + mensagem + ";" + "Processo=INICIO");
    while (threadAgent.ThreadState.ToString() != "Stopped" && DateTime.Now.Subtract(dataCheck) < TimeSpan.FromSeconds(timeout))
    {

    }

    //Verifica o resultado da consulta
    TimeSpan dataCheck2 = DateTime.Now.Subtract(dataCheck);
    TimeSpan timeoutCheck = TimeSpan.FromSeconds(timeout);
    try
    {
        if (listaServidores[listaServidores.FindIndex(s => s.Nome_Servidor == mensagem)].Status_Servidor == "OK")
        {

            Log(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + ";" + "Servidor=" + mensagem + ";" + "Processo=FIM;Status=OK");
            threadsFinalizadas++;
            totalThreadsFinalizadas++;
        }
        else if (listaServidores[listaServidores.FindIndex(s => s.Nome_Servidor == mensagem)].Status_Servidor == "ERROR")
        {

            threadsFinalizadas++;
            totalThreadsFinalizadas++;
            Servidor ServidorErro = new Servidor
            {
                Nome_Servidor = mensagem,
                Data_Consulta = listaServidores[listaServidores.FindIndex(s => s.Nome_Servidor == mensagem)].Data_Consulta,
            };
            listaServidoresErro.Add(ServidorErro);


            Log(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + ";" + "Servidor=" + mensagem + ";" + "Processo=FIM;Status=ERROR;Desc=" + listaServidores[listaServidores.FindIndex(s => s.Nome_Servidor == mensagem)].Desc);
            try
            {
                foreach (string erro in listaServidores[listaServidores.FindIndex(s => s.Nome_Servidor == mensagem)].Erros)
                {
                    ServidorErro.Erros.Add(erro);
                }
            }
            catch (Exception)
            {

                throw;
            }


        }
        else if (dataCheck2 >= timeoutCheck)
        {
            threadAgent.Interrupt();
            threadsFinalizadas++;
            totalThreadsFinalizadas++;
            servidoresErro.Add(mensagem);
            Log(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + ";" + "Servidor=" + mensagem + ";" + "Processo=FIM;Status=ERROR;Desc=Timeout:" + DateTime.Now.Subtract(dataCheck).ToString());
        }
        else
        {

            threadsFinalizadas++;
            totalThreadsFinalizadas++;
            servidoresErro.Add(mensagem);
            Log(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + ";" + "Servidor=" + mensagem + ";" + "Processo=FIM;Status=ERROR;Desc=Unknown");
        }
    }
    catch (NullReferenceException ex)
    {

        throw ex;
    }

    Log2(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + ";" + "Termino " + mensagem + " " + dataCheck2.ToString());

});

Erro

I do not understand what I need to do not occur since the variables are not null.

Can you help me?

  • The code posted does not match the image, so it gets more complicated to help, we have to shoot at a moving target. I hope that catch has been placed in despair, it is only getting in the way. http://answall.com/questions/tagged/exce%C3%A7%C3%a3o? Sort=votes&pageSize=50

  • It’s actually a piece of code, and the error happens exactly in the block I put.

  • And yes, it was in despair this catch rs, I did not understand the error if I am declaring the variables within the same block and they are not null

1 answer

-1

When Threading.Sleep(100);

Inside the While loop the problem was solved, for some reason it was generating timeout in the function.

while (threadAgent.ThreadState.ToString() != "Stopped" && DateTime.Now.Subtract(dataCheck) < TimeSpan.FromSeconds(timeout))
{
    Thread.Sleep(100);
}
  • 3

    We’re probably talking about a cross threading error then. Are the variables within the method declared outside the new threads instance? If that’s it, putting Sleep will only mask the problem

Browser other questions tagged

You are not signed in. Login or sign up in order to post.