Exceptions reading empty string from file

  • Thread starter Thread starter Claire
  • Start date Start date
C

Claire

This works ok in a new empty project. I write an empty string to a file.
Looking at the file with a hex editor, there's a single byte of value zero.
I expect this, it's utf8 encoding and this is the leading length byte.

In my "real" project I have the following code. The first MessageBox tells
me that the stream position is correct for reading this single byte. The 2nd
message box tells me that the byte count is zero for an empty string (is
this correct?. doesn't it count the leading length 7 bit char counter?)
The 4th line (line 137) is where my code falls over, as I get the exception
"System.NullReferenceException: Object reference not set to an instance of
an object.
at jfmcommsservice.PPC_Converter.ConvertUpload(FileEventArgs e, Tour tour)
in blah blah blah line 137
blah blah"

Is there an obvious problem here? Can using encoding elsewhere change things
for the rest of the application?
BinaryReader reader = new BinaryReader(e.memorystream);

System.Windows.Forms.MessageBox.Show(reader.BaseStream.Position.ToString());

System.Windows.Forms.MessageBox.Show(System.Text.Encoding.UTF8.GetByteCount("").ToString());

mm.Result.Comment = reader.ReadString();
 
Claire said:
This works ok in a new empty project. I write an empty string to a file.
Looking at the file with a hex editor, there's a single byte of value zero.
I expect this, it's utf8 encoding and this is the leading length byte.

In my "real" project I have the following code. The first MessageBox tells
me that the stream position is correct for reading this single byte. The 2nd
message box tells me that the byte count is zero for an empty string (is
this correct?. doesn't it count the leading length 7 bit char counter?)
The 4th line (line 137) is where my code falls over, as I get the exception
"System.NullReferenceException: Object reference not set to an instance of
an object.
at jfmcommsservice.PPC_Converter.ConvertUpload(FileEventArgs e, Tour tour)
in blah blah blah line 137
blah blah"

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Note that Encoding.GetByteCount certainly wouldn't include the length
prefix, as that's specific to BinaryWriter/BinaryReader, not part of
UTF-8 itself.
 
My stupid idiot self fault. I forgot to deref Result.
Sorry for using up valuable newsgroup space.
 
Back
Top