read to datagridview form binary file

shay Avital

Member
Joined
Oct 9, 2020
Messages
12
Programming Experience
Beginner
i am trying to read a binary file value in a datagridview but i get the following error:

Topshelf.Hosts.ConsoleRunHost Critical: 0 : The service threw an unhandled exception, System.NullReferenceException: Object reference not set to an instance of an object.

here is my code:

C#:
string file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"c:\qlcservice\grid.bin");
            using (BinaryReader bw = new BinaryReader(File.Open(file, FileMode.Open)))
            {
                int n = bw.ReadInt32();
                int m = bw.ReadInt32();
                for (int i = 0; i < m; ++i)
                {
                                  
                    Mlist.MeterGrid.Rows.Add();
                    for (int j = 0; j < n; ++j)
                    {
                        if (bw.ReadBoolean())
                        {
                            Mlist.MeterGrid.Rows[i].Cells[j].Value = bw.ReadString();
                        }
                        else bw.ReadBoolean();
                    }
                }
            }
the error is on line : Mlist.MeterGrid.Rows.Add();

thanks for your help,

Shay.
 
If you run with the debugger, step through your code, and hover your mouse over each identifier on that line, the debugger will tell you what the value is of each thing. You'll find out which one is null.
 
Back
Top Bottom