"对象未设置"错误 [英] "Object reference not set" error
本文介绍了"对象未设置"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有错误对象引用不设置到对象的实例。对下一个方法:
私人无效alSave_Click(对象发件人,EventArgs五)
{
_alRecord。中WriteXML(@\alRecord.xml。XmlWriteMode.WriteSchema);
}
和我不知道我能做些什么...
给的是代码:
私人字符串alFile = @。\alRecord.xml
公众的DataTable _alRecord;
私人数据表alRecord
{
得到
{
如果(_alRecord == NULL)
{
_alRecord =新的DataTable();
如果(File.Exists(alFile))
{_alRecord.ReadXml(alFile); }
,否则
{InitDataTable2(_alRecord); }
}
返回_alRecord;
}
}
私人无效InitDataTable2(DataTable的表)
{
table.TableName =阿尔塔夫莱;
table.Columns.Add(ID的typeof(INT));
table.Columns.Add(太阳的typeof(布尔));
table.Columns.Add(星期一的typeof(布尔));
table.Columns.Add(星期二的typeof(布尔));
table.Columns.Add(结婚的typeof(布尔));
table.Columns.Add(星期四的typeof(布尔));
table.Columns.Add(星期五的typeof(布尔));
table.Columns.Add(SAT的typeof(布尔));
table.Columns.Add(门,typeof运算(字符串));
table.Columns.Add(1跳转的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;我++)
{
无功行= alRecord.NewRow();
行[ID] =我;
行[太阳] = FALSE;
行[问] = FALSE;
行[星期二] = FALSE;
行[结婚] = FALSE;
行[周四] = FALSE;
行[星期五] = FALSE;
行[坐] = FALSE;
行[门] =;
行[1跳转] =00:01;
行[TO1] =23:59;
行[from2] =00:01;
行[TO2] =23:59;
alRecord.Rows.Add(行);
}
}
私人无效alSave_Click(对象发件人,EventArgs五)
{
_alRecord.WriteXml(@。\alRecord.xml,XmlWriteMode.WriteSchema );
}
解决方案
私人无效alSave_Click(对象发件人,EventArgs五)
{
_alRecord.WriteXml(@\alRecord.xml。XmlWriteMode.WriteSchema);
}
_alRecord
仅加载时,你在的属性的名称, alRecord
,因此,应写为引用它:
私人无效alSave_Click(对象发件人,EventArgs五)
{
alRecord.WriteXml(@。\alRecord.xmlXmlWriteMode.WriteSchema) ;
}
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:
private void alSave_Click(object sender, EventArgs e)
{
alRecord.WriteXml(@".\alRecord.xml", XmlWriteMode.WriteSchema);
}
这篇关于"对象未设置"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文