System.NullReferenceException,错误

时间:2018-08-08 07:45:33

标签: c# exceldatareader

namespace ProtoBuf_Parser_Nemo_
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        DataSet result;
        private void button1_Click(object sender, EventArgs e)
        {
            using(OpenFileDialog opfd = new OpenFileDialog() { Filter = "Excel Workbook 97-20016|.xls|Excel Workbook|.xlsx", ValidateNames = true })
            {
                if(opfd.ShowDialog() == DialogResult.OK)
                {
                    FileStream fs = File.Open(opfd.FileName, FileMode.Open, FileAccess.Read);
                    IExcelDataReader reader = ExcelReaderFactory.CreateBinaryReader(fs);
                    reader.IsFirstRowAsColumnNames = true;
                    result = reader.AsDataSet();
                    comboBox1.Items.Clear();

                    foreach (DataTable dt in result.Tables)
                    {
                        comboBox1.Items.Add(dt.TableName);
                        reader.Close();
                    }
                }
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataGridView.DataSource = result.Tables[comboBox1.SelectedIndex];
        }
    }
}

我在第35行foreach (DataTable dt in result.Tables)中有一个System.NullReferenceException。这是一个Windowsforms应用程序。有人可以帮助绝望的初学者吗?

0 个答案:

没有答案