抛出异常:System.NullReferenceException:对象引用未设置为对象的实例

时间:2014-12-15 06:49:05

标签: c# unit-testing exception nullreferenceexception object-reference

我收到如下错误:

  

测试失败 - CheckEffectiveQuarterForCopyModelTest消息:测试方法   TestBusinessLogic.MediaDurationBLTest.CheckEffectiveQuarterForCopyModelTest   抛出异常:System.NullReferenceException:对象引用没有   设置为对象的实例。

如何在此处修复错误(它无法在此处循环)

foreach (MediaDurationDS.TimeRow dr in accessor.mMediaDurationDataSet.Time.Rows)
{
            //dr.DateKey = dr.DateKey.ToUniversalTime();
           if (dr.DateKey != null)
            {
                dr.DateKey = dr.DateKey.ToUniversalTime();
            }


}

MediaDurationBLTest.cs文件

public void CheckEffectiveQuarterForCopyModelTest()
        {
            MediaDurationBL target = new MediaDurationBL();

            TestBusinessLogic.BusinessLogic_MediaDurationBLAccessor accessor = new TestBusinessLogic.BusinessLogic_MediaDurationBLAccessor(target);
            accessor.mMediaDurationDataSet = new MediaDurationDS();
            PopulateTestDataSet(accessor.mMediaDurationDataSet);

            foreach (MediaDurationDS.TimeRow dr in accessor.mMediaDurationDataSet.Time.Rows)
            {
                //dr.DateKey = dr.DateKey.ToUniversalTime();
            if (dr.DateKey != null)
            {
                dr.DateKey = dr.DateKey.ToUniversalTime();
            }

            else
            {
                dr.DateKey=ToUniversalTime();
            }
            }
            accessor.CheckEffectiveQuarterForCopyModel();
            accessor.mMediaDurationDataSet.AcceptChanges();

            int Expected = 1;
            int Actual = accessor.mMediaDurationDataSet.ModelTime.Rows.Count;

            Assert.AreEqual(Expected, Actual);

        }

private DateTime ToUniversalTime()
        {
            throw new NotImplementedException();
        }

SubToolBL.cs文件

public void AddEffectiveQuarter(DateTime dateKey)
    {

        SubToolDS.TimeRow TimeRow = mSubToolDataSet.Time.FindByDateKey(dateKey);

        // CHECK IF ITS DUPLICATE QUARTER; ROW STATE IS CURRENTROWS THAT ARE NOT DELETED
        if (mSubToolDataSet.ModelTime.Select("DateKey = '" + dateKey.ToString() + "'",null, DataViewRowState.CurrentRows).Length > 0 )
        {               
            string displayValue = TimeRow.DisplayValue;
            string errorMessage = displayValue+" already exists. Please select a different quarter";
            throw new ArgumentException(errorMessage);
        }

        SubToolDS.ModelTimeRow modelTimeRow = mSubToolDataSet.ModelTime.NewModelTimeRow(); ;

        modelTimeRow.ModelID = mSubToolDataSet.Model[0].ModelID;
        modelTimeRow.DateKey = dateKey;
        modelTimeRow.DisplayValue = TimeRow.DisplayValue; //error occur here
        modelTimeRow.LastUpdateDate = DateTime.Now;
        modelTimeRow.LastUpdateUserName = UtilityBL.CurrentUser.Name;
        mSubToolDataSet.ModelTime.AddModelTimeRow(modelTimeRow);

        mSubToolDataSet.ModelTime.DefaultView.Sort = "DateKey";

        int newModelTimeID = modelTimeRow.ModelTimeID;

        //NOW COPY DATA FROM PREVIOUS QUARTER
        SubToolDS.ModelTimeRow[] modelTimeRows = ((SubToolDS.ModelTimeRow[]) mSubToolDataSet.ModelTime.Select("DateKey < '" + dateKey + "'", "DateKey DESC"));

        if (modelTimeRows.Length > 0)
        {
            int sourceModelTimeID = modelTimeRows[0].ModelTimeID;

            // GET THE CORRESPONDING MODEL EVENT ROWS
            SubToolDS.ModelEventRow[] modelEventRows = (SubToolDS.ModelEventRow[]) mSubToolDataSet.ModelEvent.Select("ModelTimeID = " + sourceModelTimeID);

            foreach (SubToolDS.ModelEventRow mer in modelEventRows)
            {
                this.AddEvent(mer, dateKey);
            }
        }

    }

0 个答案:

没有答案