I get the error as below:
Test Failed-CheckEffectiveQuarterForCopyModelTest Message: Test method TestBusinessLogic.MediaDurationBLTest.CheckEffectiveQuarterForCopyModelTest threw exception: System.NullReferenceException:Object reference not set to an instance of an object.
How do I fix the error at here (it cannot get out the loop at here)
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 file
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 file
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);
}
}
}