Hi all! I'm having issues with a basic C# program, on my first attempt. I'm getting a null object not instantiated error when I attempt to ReadByte in the second procedure:
public void Main(string FileName)
{
System.IO.FileStream FS = new System.IO.FileStream(FileName, System.IO.FileMode.Open);
System.IO.BinaryReader BR = new System.IO.BinaryReader(FS);
ReadHeader(ref BR);
BR.Close();
FS.Close();
}
private void ReadHeader(ref System.IO.BinaryReader BR)
{
for (int ii = 0; ii < 32; ii++)
{
MarkerInfo.Header.bB1_32[ii] = BR.ReadByte();
}
}
Any help, much appreciated!
Loading
Jan MontanoPosted Feb 1, 2008, 12:32 AM
Cheers.
StevePosted Jan 31, 2008, 3:35 PM
I have got another issue now, but I don't think it is worthy of a new topic. I am now exporting the binary data to file using BinaryWriter and I'm just outputting the exact same data I have read in using BinaryReader. Stepping through the program, everything appears to be in order but when I check the binary file, extra zeros are appearing at seemingly random places.
Has anyone seen this sort of thing with BinaryWriter before?
Jan MontanoPosted Jan 30, 2008, 1:32 AM
public partial class Form1 : Form
{
MM_Format_Class MarkerInfo = new MM_Format_Class();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Process();
}
private void Process()
{
System.IO.FileStream FS = new System.IO.FileStream(@"c:\test.txt", System.IO.FileMode.Open);
System.IO.BinaryReader BR = new System.IO.BinaryReader(FS);
ReadHeader(ref BR);
BR.Close();
FS.Close();
}
private void ReadHeader(ref System.IO.BinaryReader BR)
{
for (int ii = 0; ii < 32; ii++)
{
MarkerInfo.Header.bB1_32[ii] = BR.ReadByte();
}
}
}
Could you try hardcoding the filename instead? and put 50 characters on the file.
StevePosted Jan 30, 2008, 1:07 AM
I'm not sure exactly what I'm doing wrong though. This is how I declare the byte array type:
public class MM_Header_Class
{
public byte[] bB1_32 = new byte[32];
}
and the parent class:
public class MM_Format_Class
{
public MM_Header_Class Header = new MM_Header_Class();
}
and in an initialise function the main variable:
MM_Format_Class MarkerInfo = new MM_Format_Class();
but I still get the same issue. I know it must be something obvious, can you see it?
Jan MontanoPosted Jan 30, 2008, 12:31 AM
byte[] MarkerInfo.Header.bB1_32 = new byte[32];
I tried the code below and it works fine.
private void Process()
{
System.IO.FileStream FS = new System.IO.FileStream(@"c:\test.txt", System.IO.FileMode.Open);
System.IO.BinaryReader BR = new System.IO.BinaryReader(FS);
ReadHeader(ref BR);
BR.Close();
FS.Close();
}
private void ReadHeader(ref System.IO.BinaryReader BR)
{
byte[] bytearray = new byte[32];
for (int ii = 0; ii < 32; ii++)
{
bytearray[ii] = BR.ReadByte();
}
}