hipswich

I have this error occurring under strange circumstances. I hope someone can help explain it.

The code is very simple and looks essentially like the following

//alData is an instance of ArrayList storing byte data

if (alData.Count > 0)

{

object[] ao = alData.ToArray();

alData.Clear();

try

{

foreach (byte b in ao)

{

//some code not using either b or ao just for debugging

}

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

The catch statement frequently catches the error of "Object reference not set to an instance of an object". How can this possibly happen




Re: Visual C# Language Object reference not set to an instance of an object for an array

Nimrand

First, look at the stack trace and find out what line the exception is being thrown on. This will narrow down the possibilities.




Re: Visual C# Language Object reference not set to an instance of an object for an array

hipswich

I am sorry but I forgot to mention the line that throws the exception is foreach (byte b in ao).






Re: Visual C# Language Object reference not set to an instance of an object for an array

Nimrand

Then I don't see how that is possible either. Are you sure that the stack trace isn't off by a line number or two. I've noticed that happens sometimes.




Re: Visual C# Language Object reference not set to an instance of an object for an array

hipswich

The following code may be more convincing. I added it just for the sake of debugging

for (int i = 0; i < ao.Length; ++i)

{

try

{

byte b = (byte)aoIdea;

}

catch (Exception ex)

{

MessageBox.Show(ex.Message + " for byte b = aoIdea");

}

}

The red highlighted line throws the exception frequently. When I check aoIdea when there is an exception, it is is null.






Re: Visual C# Language Object reference not set to an instance of an object for an array

Nimrand

The class description for ArrayList states that ArrayList accepts nulls as elements. The only explanation is that your ArrayList has nulls in it, and you're getting a NullReferenceException when you try to convert the null value to byte.




Re: Visual C# Language Object reference not set to an instance of an object for an array

hipswich

It is a good point. I will check it next Monday because I do not have access to the code now.

The ArrayList instance should not have any nulls because only byte data are added to it but I will examine the code carefully.





Re: Visual C# Language Object reference not set to an instance of an object for an array

hipswich

It is exactly the case - the ArrayList al has nulls.

Here is the code that is the only place where al gets its elements.

//al is an instance of ArrayList, abPacket is byte[]

al.AddRange(abPacket);

foreach (object o in al)

{

//the following code is for debugging

if (o == null)

{

MessageBox.Show("(o == null)");

break;

}

}

abPacket stores byte values, how can al have nulls






Re: Visual C# Language Object reference not set to an instance of an object for an array

eradicator

al.AddRange(abPacket);

And where else are you adding elements to al I have to guess that somewhere you did indeed add a null item to it, not on this line.




Re: Visual C# Language Object reference not set to an instance of an object for an array

hipswich

I searched the code thoroughly and that's the only place where the ArrayList instance's elements are added. This is a small application with less than 3000 source lines.






Re: Visual C# Language Object reference not set to an instance of an object for an array

Nimrand

I would replace ArrayList with something that doesn't allow nulls. If you aren't using 2.0, you might consider writing your own strongly typed collection. That will identify where the problem is on the spot.

Beyond that, all you can do it step through your program and watch what happens to the ArrayList so you can determine when the null is inserted.




Re: Visual C# Language Object reference not set to an instance of an object for an array

hipswich

I have been testing it for hours without seeing that error again so I am reasonable sure the problem has been solved. The error used to show up in minutes.

I replaced

al.AddRange(abPacket);

with

foreach (byte b in abPacket)

{

al.Add(b);

}

Could anyone tell me the effective difference between these two ways of copying data from an array to an ArrayList and why the former one may generate nulls in the ArrayList