0

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);
            }
        }

    }
iop
  • 39
  • 1
  • 7
  • i have look through this and a few forum for this error, but still could not solve this error, what should i do for the foreach? I am first time doing this... – iop Dec 15 '14 at 06:59
  • One of your `MediaDurationDS.TimeRow` objects in your `accessor.mMediaDurationDataSet.Time.Rows` collection is null and you try to do this: `null.ToUniversalTime();` This is as simple as that – Mikayil Abdullayev Dec 15 '14 at 07:14
  • u means like this "dr.DateKey = dr.DateKey.null.ToUniversalTime();"? – iop Dec 15 '14 at 07:43
  • No, I mean DataKey is null itself. – Mikayil Abdullayev Dec 15 '14 at 07:55
  • how do i put this? may you explain more details? – iop Dec 15 '14 at 07:59
  • Ok, I'll try to help you. Can you check if you get this exception inside the loop or in this part `accessor.mMediaDurationDataSet.Time.Rows` – Mikayil Abdullayev Dec 15 '14 at 08:07
  • i get that exception inside the loop... – iop Dec 15 '14 at 08:11
  • Ok, then either the DateKey is a Nullable (or DateTime? in short) and you get this exception when you try to invoke ToUniversalTime method of something that is null. If this is not the case, then somehow the `dr` itself is null and you're trying to set/get DateKey property of null. If the collection contains only few elements then you can just debug it and find out which one causes the problem. Or if it's huge, then you can put a condition inside the loop to check if if dr or DateKey is null and output that particular one. – Mikayil Abdullayev Dec 15 '14 at 08:17
  • my "DateKey" is null, then what should i do with "null.ToUniversalTime();"? It return me the error. While the code keep on looping within the foreach loop, it cannot get out of the loop to continue the following code. May you post out the code step by step that what should I do. I understand what do u mean, but I am first time handling this, really have no idea...thx – iop Dec 16 '14 at 02:57
  • So, what I understand is your DateKey is a Nullable.Technically you do lots of things, but the simplest and the most normal one would be to put an if statement like this: `if(dr.DateKey!=null) dr.DateKey = dr.DateKey.ToUniversalTime();` So you check if DateKey is null and if it's not you set it to be universal time, otherwise you pass it. And you're good to go. – Mikayil Abdullayev Dec 16 '14 at 05:11
  • @MikeJM, I have post the latest code as above, is it that the way you mention? I still cannot pass my test case. Is that anything that I have wrongly did? – iop Dec 16 '14 at 06:14
  • the error occur at "modelTimeRow.DisplayValue = TimeRow.DisplayValue;" in SubToolBL.cs file – iop Dec 16 '14 at 06:27
  • what is this line? ` else {dr.DateKey=ToUniversalTime();}. There are no standalone methods in c#. You've used ToUniversalTime() as if it does not belong to any class. That line is wrong. ToUniversalTime converts a given time to UTC (Coordinated Universal Time). This code cannot compile.Either remove the else part or do somethig like this. else {dr.DateKey=DateTime.Now.ToUniversalTimme();} – Mikayil Abdullayev Dec 16 '14 at 06:44
  • hi, i have try both of the method that you mention above, but I could not get out of the loop, it keep on running within the foreach. – iop Dec 17 '14 at 03:04
  • Then, the only thing I can offer is you can send the the part of the project so that I can see the real problem. – Mikayil Abdullayev Dec 17 '14 at 04:03
  • hi, I have post part of the project as above, it cannot get out the loop at "MediaDurationBLTest.cs file" and undergo the following loop, I have also try to put else {dr.DateKey=DateTime.Now.ToUniversalTimme();}, – iop Dec 17 '14 at 04:11
  • `List strings = new List();strings.Add("Foo");strings.Add("Bar");strings.Add(null); strings.Add("AnotherFoo");strings.Add("AnotherBar"); foreach (var item in strings) {Console.WriteLine(item.Substring(1));}` Just create a new console applicaiton and put this piece of code inside your Main method. You will get "oo" and "ar" but then you're going to receive the "Null Reference Exception". It's because The third element of the "strings" list is null so this line `Console.WriteLine(item.Substring(1));` becomes this Console.WriteLine(null.Substring(1)); – Mikayil Abdullayev Dec 17 '14 at 04:21
  • null.Substring(1) causes the exception. As the name suggests, you're trying to reference an object, or piece of memory to be more precise that does not exist. So to draw analogy with your case, what I understand is one or more of the `dr`s are null. So when you say `dr.DateKey` it throws exception because dr itself is null. This means you should change `if(dr.DateKey!=null)` to `if(dr!=null)`. – Mikayil Abdullayev Dec 17 '14 at 04:31
  • it work the same as previous, now i put the "break;" to get it out of the foreach loop, however the error link to the other pass unit test say that "Object reference not set to an instance of an object." at the "modelTimeRow.DisplayValue = TimeRow.DisplayValue;" – iop Dec 18 '14 at 00:40

0 Answers0