“对象引用未设置”错误

时间:2010-08-15 13:54:17

标签: c# .net

我有错误“对象引用未设置为对象的实例”。在下一个方法:

        private void alSave_Click(object sender, EventArgs e)
    {
        _alRecord.WriteXml(@".\alRecord.xml", XmlWriteMode.WriteSchema);
    }

我不知道我能做什么...... 这是代码:

        private string alFile = @".\alRecord.xml";

    public DataTable _alRecord;
    private DataTable alRecord
    {
        get
        {
            if (_alRecord == null)
            {
                _alRecord = new DataTable();
                if (File.Exists(alFile))
                { _alRecord.ReadXml(alFile); }
                else
                { InitDataTable2(_alRecord); }
            }
            return _alRecord;
        }
    }

    private void InitDataTable2(DataTable table)
    {
        table.TableName = "AlTable";
        table.Columns.Add("ID", typeof(int));
        table.Columns.Add("sun", typeof(bool));
        table.Columns.Add("mon", typeof(bool));
        table.Columns.Add("tue", typeof(bool));
        table.Columns.Add("wed", typeof(bool));
        table.Columns.Add("thu", typeof(bool));
        table.Columns.Add("fri", typeof(bool));
        table.Columns.Add("sat", typeof(bool));
        table.Columns.Add("doors", typeof(string));
        table.Columns.Add("from1", typeof(DateTime));
        table.Columns.Add("to1", typeof(DateTime));
        table.Columns.Add("from2", typeof(DateTime));
        table.Columns.Add("to1", typeof(DateTime));
        for (int i = 0; i < 99; i++)
        {
            var row = alRecord.NewRow();
            row["ID"] = i;
            row["sun"] = false;
            row["mon"] = false;
            row["tue"] = false;
            row["wed"] = false;
            row["thu"] = false;
            row["fri"] = false;
            row["sat"] = false;
            row["doors"] = "";
            row["from1"] = "00:01";
            row["to1"] = "23:59";
            row["from2"] = "00:01";
            row["to2"] = "23:59";
            alRecord.Rows.Add(row);
        }
    }
    private void alSave_Click(object sender, EventArgs e)
    {
        _alRecord.WriteXml(@".\alRecord.xml", XmlWriteMode.WriteSchema);
    }

3 个答案:

答案 0 :(得分:5)

private void alSave_Click(object sender, EventArgs e) 
    { 
        _alRecord.WriteXml(@".\alRecord.xml", XmlWriteMode.WriteSchema); 
    } 

_alRecord仅在您通过属性名称alRecord引用时加载,因此应写为:

private void alSave_Click(object sender, EventArgs e) 
    { 
        alRecord.WriteXml(@".\alRecord.xml", XmlWriteMode.WriteSchema); 
    } 

答案 1 :(得分:2)

您正在使用一种称为延迟初始化的技术。在调用alRecord之前,不会分配_alRecord。

您的alSave_Click方法使用了错误的alRecord ...它应该使用没有下划线前缀的那个。即。

private void alSave_Click(object sender, EventArgs e) 
{ 
    alRecord.WriteXml(@".\alRecord.xml", XmlWriteMode.WriteSchema); 
} 

作为旁注,你的alReord声明的范围似乎是错误的方式(按惯例)。

public DataTable _alRecord; //Normally **private** members would have an underscore prefix    
private DataTable alRecord  //And public members would have no prefix.

修改

顺便说一句,一旦_alRecord被分配,理论上你可以直接使用它。然而,这违背了懒惰初始化模式的目的。

E.g。以下也可以,但关键是你不想担心_alRecord是否已初始化,你只想使用它;如果它恰好是您第一次使用它,那么它将自动初始化。

private void alSave_Click(object sender, EventArgs e) 
{ 
  alRecord;
  _alRecord.WriteXml(@".\alRecord.xml", XmlWriteMode.WriteSchema); 
} 

答案 2 :(得分:1)

编辑:

public DataTable m_alRecord;// don't use it
private DataTable alRecord
{
    get
    {
        if (m_alRecord == null)
        {
            m_alRecord = new DataTable();
            if (File.Exists(alFile))
            { m_alRecord.ReadXml(alFile); }
            else
            { InitDataTable2(m_alRecord); }
        }
        return _alRecord;
    }
    set { m_alRecord = value; }
}