Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi,

I am using list int as buffer in my program

I need to check a condition after that I need to make buffer empty

C#
if (buffer1.Count != 0)
{
    int largestVal = buffer1[0];
    foreach (int temp in buffer1)
    {
       if (temp > largestVal)
         largestVal = temp;
       f2 = largestVal;
    }

    for (int z = f2 + 1; z < k - 1; z++)
    {
        if ((CANMsgIdList[i].MsgId == CANMsgIdList[z].MsgId))
        {
            buffer2[q2] = z;
        }
    }
 }
 //buffer1 = null;


The whole code is running in a for loop.
I am getting null exception at the line (buffer1.Count != 0)
Error: Object reference not set to an instance of an object.
Posted
Updated 7-Jan-14 3:27am
v2

If buffer1 is a List<int> </int>you have to call buffer1.Clear() in order to empty it.
 
Share this answer
 
If you are getting a null reference error on the line:
C#
if (buffer1.Count != 0)

Then the only possible reason is that butter1 is null: i.e. you have not assigned a value to it anywhere. Try looking at all the references to it, and look for either places where you assign buffer1 (and the assigned value could be null) or for somewhere in the code where you should be writing something like:
C#
buffer1 = new List<int>();
but aren't!
 
Share this answer
 
Comments
bowlturner 7-Jan-14 9:34am    
I had missed the List. I saw your answer, reread the problem and deleted mine when I saw my mistake.
OriginalGriff 7-Jan-14 9:43am    
:thumbsup:
No problem - we all do it from time to time!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900