【发布时间】:2013-10-16 08:58:03
【问题描述】:
我有如下代码:
try
{
strReportName = Application.StartupPath + "\\Report\\Accounts\\AccTrialBalanceCrystalReport.rpt";
DataSet ds = new System.Data.DataSet();
SchoolSoulLibrary.clsCommonVariables OClsCommonVariables = new SchoolSoulLibrary.clsCommonVariables();
ds = OclsCommonVariables.SetDataInDataSetFromEnumerableList(ref ds, reportData.AsEnumerable()); // Throws exception at this line.
string[,] AryParameter = new string[,]
{
{"totalOpeningDr", vOpDr.ToString()},
{"totalOpeningCr", vOpCr.ToString()},
{"totalCurrentDr", vCurDr.ToString()},
{"totalCurrentCr", vClsngDr.ToString()},
{"totalClosingDr", vCurCr.ToString()},
{"totalClosingCr", vClsngCr.ToString()},
{"schoolName", clsSchoolSoulObjects.OAcdSchoolInfo.SchoolName},
{"@pStartDate", startDate.ToString()},
{"@pEndDate", endDate.ToString()},
{"@pSchoolId", schId.ToString()},
};
SchoolSoulLibrary.clsCrystalReport.SetReportSourceUsingReportPath(strReportName, ds, ref crystalReportViewer1, AryParameter);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
在这一行
ds = OclsCommonVariables.SetDataInDataSetFromEnumerableList(ref ds, reportData.AsEnumerable());
程序没有进入函数内部就抛出异常
抛出的异常是“对象引用未设置为对象实例”。
函数需要三个参数
public DataSet SetDataInDataSetFromEnumerableList(ref DataSet DS, object obj, params string[] FieldNames)
{
return ds;
}
【问题讨论】:
-
您检查过
reportData是否为空吗? -
不调用为什么会有代码?你怎么知道确定它没有被调用?您是否在违规行设置了断点?
-
能否请您说明错误是在哪里引发的? ...也许也发布该代码并指出它是哪一行?
-
NullReferenceException的几乎所有情况都是相同的。请参阅“What is a NullReferenceException in .NET?”获取一些提示。 -
@Chris Skardon:是的,我检查了报告数据不为空。 @ knittl:我在该行设置了断点,我正在调用该函数,再次查看代码。 @ user2042152 :它在这一行抛出异常 ds = OclsCommonVariables.SetDataInDataSetFromEnumerableList(ref ds, reportData.AsEnumerable());
标签: c# function exception-handling nullreferenceexception