A
A
aeaeae12020-12-27 09:10:26
C++ / C#
aeaeae1, 2020-12-27 09:10:26

Object reference does not point to an instance of an object, error. How to fix?

Hello! !Tell me PLEASE WHAT IS THE PROBLEM? Gives an error
System.NullReferenceException: "Object reference does not point to an instance of an object."
result was null.

using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Excel Files|*.xls;*.xlsx;*.xlsm" })
            {
                if (ofd.ShowDialog() == DialogResult.OK)
                {
 
 
                 
                    FileStream fs = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read);
                    Excel.IExcelDataReader reader = Excel.ExcelReaderFactory.CreateBinaryReader(fs);
                    reader.IsFirstRowAsColumnNames = true;
                    result = reader.AsDataSet();
                    CLEAR.Items.Clear();
                    
                    foreach (DataTable dt in result.Tables)
                    {
                        dt.TableName = "tab";
                        CLEAR.Items.Add(dt.TableName);
 
                    }
                    // dataGridView1.DataSource = dt.DefaultView;
 
                    reader.Close();
 
 
 
                }
            }
 
        }

Here is this line And moreover, when the xls file is doing everything is fine, and when xlsx gives an error
foreach (DataTable dt in result.Tables)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ananiev, 2020-12-27
@aeaeae1

Your library only works with the xls format, or other methods are used to work with the xlsx format. Read the documentation for your Excel library.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question