[英]“Object reference not set” error
I have the error "Object reference not set to an instance of an object." 我有错误“对象引用未设置为对象的实例”。 on the next method:
在下一个方法:
private void alSave_Click(object sender, EventArgs e)
{
_alRecord.WriteXml(@".\alRecord.xml", XmlWriteMode.WriteSchema);
}
and i don't know what can i do... here is the code: 我不知道我能做什么...这里是代码:
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);
}
private void alSave_Click(object sender, EventArgs e)
{
_alRecord.WriteXml(@".\alRecord.xml", XmlWriteMode.WriteSchema);
}
_alRecord
is only loaded when you reference it by the property name, alRecord
, hence that should be written as: _alRecord
仅在您通过属性名称alRecord
引用时加载,因此应写为:
private void alSave_Click(object sender, EventArgs e)
{
alRecord.WriteXml(@".\alRecord.xml", XmlWriteMode.WriteSchema);
}
You're using a technique called lazy initialization. 您正在使用一种称为延迟初始化的技术。 _alRecord is not assigned until alRecord has been called.
在调用alRecord之前,不会分配_alRecord。
Your alSave_Click method is using the wrong alRecord... it should be using the one without the underscore prefix. 您的alSave_Click方法使用了错误的alRecord ...它应该使用没有下划线前缀的那个。 Ie
即
private void alSave_Click(object sender, EventArgs e)
{
alRecord.WriteXml(@".\alRecord.xml", XmlWriteMode.WriteSchema);
}
As a side note, the scope of your alReord declarations seem to be the wrong way round (by convention that is). 作为旁注,你的alReord声明的范围似乎是错误的方式(按惯例)。
public DataTable _alRecord; //Normally **private** members would have an underscore prefix
private DataTable alRecord //And public members would have no prefix.
By the way, once _alRecord has been assigned, then theoretically you could use it directly. 顺便说一句,一旦_alRecord被分配,理论上你可以直接使用它。 However that defeats the purpose of the lazy initialization pattern.
然而,这违背了懒惰初始化模式的目的。
Eg The following would also work, but the point is you don't want to worry about whether _alRecord has been initialized, you just want to use it; 例如以下也可以,但关键是你不想担心_alRecord是否已被初始化,你只想使用它; and if it happens to be the first time you're using it, then it will be automatically initialized.
如果它恰好是您第一次使用它,那么它将自动初始化。
private void alSave_Click(object sender, EventArgs e)
{
alRecord;
_alRecord.WriteXml(@".\alRecord.xml", XmlWriteMode.WriteSchema);
}
Edit: 编辑:
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; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.