-3

I know this will probably will get marked as duplicate, but I have seen other questions and they don't clarify me. This is on one thread:

        int indexOfList = 0;
        long byteSent = 0;
        byte[] chunkToSend;

        while(bytesThatAreCompressed != byteSent || !finished)
        {
            if (indexOfList < listOfChunksToSend.Count)
            {
                int testinteger = listOfChunksToSend[indexOfList ].Length;
                chunkToSend= new byte[testinteger];
                chunkToSend= listOfChunksToSend[indexOfList ];

                int tamanhoBufferComprimido = (int)chunkToSend.Length;
                byte[] tamanhoChunkB = BitConverter.GetBytes(tamanhoBufferComprimido);
                streamligacao.Write(tamanhoChunkB, 0, tamanhoChunkB.Length);

                streamligacao.Write(chunkToSend, 0, tamanhoBufferComprimido);
                byteSent += chunkToSend.Length;
                indexOfList ++;
            }
        }

And this is on another thread:

listOfChunksToSend.Add(stream.ToArray());   

It is throwing the exeption:

The object reference not set to an instance of an object .

listOfChunksToSend is a list of byte[]

When it throws this exception I check the variables and there is nothing wrong!

What am I doing wrong?

meme
  • 597
  • 1
  • 10
  • 23
  • 1
    Post all relevant code and show the line that the exception is thrown on. – Luke Joshua Park Apr 07 '16 at 10:58
  • Could you translate all occurences of your variables? you're initializing `indexOfList` and your last line reads `indexDaLista++`. We possible could infer the meaning, but we can't be sure. – Marco Apr 07 '16 at 11:00
  • I have updated the code and after 1 hour of searching i have found he problem. i will put it in the answers. – meme Apr 07 '16 at 11:13
  • It is not a duplicate! That question has really vague answers! – meme Apr 07 '16 at 11:20
  • This was a specific problem of using a **list** and different threads. When adding items to a list it increments the index at the start even if the adding of data is not done yet. So if you are using different thread they will not work right. That is the reason why you have to use a variable to store a index. and increment that variable after the item is added to the list! – meme Apr 07 '16 at 11:24

1 Answers1

0

I have found the answer myself..

This is happening because of confusing between threads. I have since added a variable to store the index of the list. So that only after the stream is successfully added to the byte[] list will the index be incremented.

More explanation: This was a specific problem of using a list and different threads. When adding items to a list it increments the index at the start even if the adding of data is not done yet. So if you are using different thread they will not work right. That is the reason why you have to use a variable to store a index. and increment that variable after the item is added to the list!

Code change:

Thread 1:

 int indexOfList = 0;
        long byteSent = 0;
        byte[] chunkToSend;

        while(bytesThatAreCompressed != byteSent || !finished)
        {
            if (indexOfList < lastAvailableIndex )
            {
                int testinteger = listOfChunksToSend[indexOfList ].Length;
                chunkToSend= new byte[testinteger];
                chunkToSend= listOfChunksToSend[indexOfList ];

                int tamanhoBufferComprimido = (int)chunkToSend.Length;
                byte[] tamanhoChunkB = BitConverter.GetBytes(tamanhoBufferComprimido);
                streamligacao.Write(tamanhoChunkB, 0, tamanhoChunkB.Length);

                streamligacao.Write(chunkToSend, 0, tamanhoBufferComprimido);
                byteSent += chunkToSend.Length;
                indexOfList ++;
            }
        }

Thread 2:

listOfChunksToSend.Add(stream.ToArray());   
lastAvailableIndex = listOfChunksToSend.Count - 1;
meme
  • 597
  • 1
  • 10
  • 23