none
Getting Error Object reference not set to an instance of an object when populating data table RRS feed

  • Question

  • see the code

    DataTable dt = new DataTable();
    
                var AllQuater = _FundamData.Where(b=> b.AnnualQuater=="Q").Select(a=> a.Period).Distinct()
                .OrderBy(s => Regex.Match(s, @"\d\d\d\d").Value)
                .ThenBy(s => Regex.Match(s, @"^.Q").Value + '5')
                .ThenBy(s => !Regex.Match(s, @"FYA$").Success)
                .ThenBy(s => !Regex.Match(s, @"FY$").Success)
                .ToList().TakeLast(8);
    
                var AllFY = _FundamData.Where(b => b.AnnualQuater == "A").Select(a => a.Period).Distinct()
                .OrderBy(s => Regex.Match(s, @"\d\d\d\d").Value)
                .ThenBy(s => Regex.Match(s, @"^.Q").Value + '5')
                .ThenBy(s => !Regex.Match(s, @"FYA$").Success)
                .ThenBy(s => !Regex.Match(s, @"FY$").Success)
                .ToList().TakeLast(2);
    
                try
                {
                    dt.Columns.Add("FORM NAME", typeof(string));
                    dt.Columns.Add("As Reported Date", typeof(string));
                    dt.Columns.Add("StandardCode", typeof(string));
                    dt.Columns.Add("StandardName", typeof(string));
                    dt.Columns.Add("AsReportedCode", typeof(string));
                    dt.Columns.Add("AsReportedName", typeof(string));
    
                    foreach (var q in AllQuater)
                    {
                        dt.Columns.Add(q, typeof(string));
                    }
    
                    foreach (var fy in AllFY)
                    {
                        dt.Columns.Add(fy, typeof(string));
                    }
    
                    foreach (var item in _FundamData)
                    {
                        DataRow row = dt.NewRow();
                        row["FORM NAME"] = item.FORMNAME;
                        row["As Reported Date"] = item.AsReported_Date;
                        row["StandardCode"] = item.StandardCode;
                        row["StandardName"] = item.StandardName;
                        row["AsReportedCode"] = item.AsReportedCode;
                        row["AsReportedName"] = item.AsReportedName;
                        row[dt.Columns[item.Period].Ordinal] = item.Value;
    
                        //row[row.Table.Columns["ColumnName"].Ordinal + 1] = someOtherValue;
                        dt.Rows.Add(row);
                    }
                }
                catch(Exception ex)
                {
    
                }
    this line throwing error row[dt.Columns[item.Period].Ordinal] = item.Value;

    how to fix it. thanks

    Monday, October 21, 2019 3:46 PM

Answers

All replies

  • An item should have null value check against it, if it can be null, before you try to make a row column data out of it, becuase null valued data cannot be turned into a object due to null represents a nothing it doesn't exist.

    You can us the ternary operator.

    https://www.tutorialsteacher.com/csharp/csharp-ternary-operator

    An example..

    row["AsReportedName"] = item.AsReportedName == null ? "" : item.AsReportedName;

    row[dt.Columns[item.Period].Ordinal] = item.Value

    Also, the above statement makes no sense the whole ordinal thing, which can be a problem too as no object can be made.

    • Edited by DA924x Monday, October 21, 2019 4:21 PM
    Monday, October 21, 2019 4:14 PM
  • Here is another option

    row[dt.Columns[item.Period].Ordinal] = item.Value ?? "";


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, October 21, 2019 5:01 PM
    Moderator
  • Hi Sudip,

    Put a breakpoint on that line and see what the values of each variable in that line are. One of them is going to be null, and that's what you'll need to do something about before executing that line.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Tuesday, October 22, 2019 5:04 AM
    Moderator
  • Hi Sudip_inn, 

    Thank you for posting here.

    According to your description, I have a question to confirm with you.

    Could you provide some code about ‘_FundamData’ and more information about your exception? It will help us to analyze your problem and make a test.

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 22, 2019 5:28 AM
    Moderator
  • i have fix the error this way

    if (dt.Columns.Contains(item.Period))
    {
        row[dt.Columns[item.Period].Ordinal] = item.Value;
    }

    Tuesday, October 22, 2019 8:15 AM
  • Hi Sudip_inn,

    It seems that your problem has been solved. If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Thank you for your support.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 22, 2019 9:56 AM
    Moderator