[Mono-devel-list] List<byte>.AddRange throws NullReferenceException

Samuel Kaufman emrysk at gmail.com
Sat May 14 18:03:45 EDT 2005

I'm using Mono 1.1.7 built from source on Ubuntu (Hoary).

The following code throws a NullReferenceException from AddRange. 
I've checked my variables and none are null.

    public byte[] ToData()
        List<byte> buffer = new List<byte>(96);
        byte[] b1 = Encoding.ASCII.GetBytes("Electricman");
        return buffer.ToArray();

Here's the dump:

$ gmcs hi.cs User.cs
$ mono hi.exe

Unhandled Exception: System.NullReferenceException: A null value was
found where an object instance was required.
in <0x00022> System.Collections.Generic.List`1<System.Byte>:AddRange
(IEnumerable`1 )
in <0x00050> LoginPacket:ToData ()
in <0x00012> LoginPacket:Send (System.Net.Sockets.Socket socket)
in <0x0009c> Program:Main ()

This is the odd bit, though.  If I call:   foreach (byte b in b1)
buffer.Add(b);   instead of AddRange, it works just fine.

Looking at Mono's source, AddRange is implemented in this chunk of
code, which should be identical to what I'm doing with foreach:

[MonoTODO ("PERFORMANCE: fix if it is an IList <T>")]
public void AddRange(IEnumerable<T> collection)
	foreach (T t in collection)
		Add (t);

My first intuition is that this is a bug with IEnumerable<byte> in
Mono, but I wanted to post here before filing a bug report.  (I'm not
totally sure I'm not just doing something stupid.)  Has anybody else
had the same problem?

More information about the Mono-devel-list mailing list