-2

When I run my program the code below comes up with an error:

///////////////////////////// Read in the selected //////////////
BinaryReader br2 = new BinaryReader(File.OpenRead(directoryToSearch2),
                                    System.Text.Encoding.BigEndianUnicode);

var Hexbytes = new List<string>();

///////////////////////////// Read in offset ////////////////
for (int a = 0x12 ; a <= 0x26; a++)
{
    br.BaseStream.Position = a;
    Hexbytes.Add(br.ReadByte().ToString("X2"));
}

The main line the debugger doesn't like is this br.BaseStream.Position = a;.

It states that "NullReferenceException was unhandled" and "Object reference not set to an instance of an object."

Any ideas what is going wrong?

Otiel
  • 18,404
  • 16
  • 78
  • 126
user826436
  • 231
  • 1
  • 4
  • 14
  • 2
    where is br defined in your code section – Richard Forrest Dec 02 '11 at 21:40
  • 2
    Can we see the definition for br? Or did you mean br2? – MGZero Dec 02 '11 at 21:41
  • I meant br2 ... I never realised until you pointed out that it is meant to be br2 ... I feel so stupid, specially I have sat here for over an hour :( ... I suppose that is what you get for badly naming variables .... Thank you so much dude – user826436 Dec 02 '11 at 21:44
  • Doesn't `br.ReadByte()` increment the position by one byte anyways? You shouldn't have to change the position each time. – Danny Dec 02 '11 at 21:44

2 Answers2

1

With the code you posted, we can't tell what br is. A NullReferenceException is cased when one of the objects used is null. In the case of your exception, the underlying BaseStream could be null or br itself is null.

When the debugger breaks, hover over br and see what it shows you, if it isn't null, scroll through the properties to find BaseStream and see if that is null.

My guess? You create a new BinaryReader object called br2 and are accessing the .BaseStream.Position property from br.

Joshua
  • 8,112
  • 3
  • 35
  • 40
0

Because br or br.BaseStream is null.

burning_LEGION
  • 13,246
  • 8
  • 40
  • 52
Steve Wellens
  • 20,506
  • 2
  • 28
  • 69