【问题标题】:Function never gets called and throws exception函数永远不会被调用并抛出异常
【发布时间】: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


【解决方案1】:

您需要检查报告数据。这将是 null 这就是它抛出异常的原因。或者它可能是 SetDataInDataSetFromEnumerableList 方法。调试这个方法,看看到底什么是 null。

【讨论】:

    【解决方案2】:

    使用异常的堆栈跟踪,Luc

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-05
      • 2016-09-30
      • 2017-12-17
      相关资源
      最近更新 更多