Creating HTML table from List throws null exception (Printing to console works)

0

Here is the method for generating HTML table:

   public static string CreateHTMLTable<T>(IEnumerable<T> list, params Func<T, (object, string)>[] fxns) // Used for generating HTML tables for Outlook
    {
        var sb = new StringBuilder();
        sb.Append("<table>\n");
        sb.Append("<thead>\n");
        sb.Append("<tr>\n");

        foreach (var fxn in fxns)
            sb.Append("<th>").Append(fxn(default).Item2).AppendLine("</th>");

        sb.Append("</tr>\n");
        sb.Append("</thead>\n");

        foreach (var item in list)
        {
            sb.Append("<tr>\n");
            foreach (var fxn in fxns)
            {
                sb.Append("<td>");
                sb.Append(fxn(item).Item1);
                sb.Append("</td>");
            }
            sb.Append("\n</tr>\n");
        }
        sb.Append("</table>");

        return sb.ToString();
    } 

I have Linq:

        var ComparedProjectsList = MainProcess.Projects.AsEnumerable()
            .GroupBy(x => x.Field<string>("PROJECTNUMBER").Trim())
            .Select(x => new
            {
                PROJECTNUMBER = x.Key,
                PROJECTNAME = x.Select(y => y.Field<string>("PROJECTNAME").Trim()),
                user = x.Select(y => y.Field<string>("PROJECTMANAGER").Trim()).Distinct().ToList()
            })
            .Where(x => x.user.Count > 1)
            .Select(x => new
            {
                PROJECTNUMBER = x.PROJECTNUMBER,
                PROJECTNAME = x.PROJECTNAME.First().ToString(),
                ProjectManager = x.user[0],
                ProjectManager2 = x.user[1]
            })
            .ToList();

Just for checking that there are actually values:

        foreach (var item in ComparedProjectsList)
        {
            Console.WriteLine("{0} - {1} {2} / {3}", item.PROJECTNUMBER, item.PROJECTNAME, item.ProjectManager, item.ProjectManager2);
        }

I get in console:

123456 - PROJECT 1 JOHN SMITH / DONALD RED
235454 - PROJECT 2 LIINOS TORVALDS / FUJI BANKS
456546 - PROJECT 3 STEVEN SEAGAL / TOM HAWK

I am using this for generating HTML table:

        var ComparedHTMLTableProject = CreateHTMLTable,
            x => (x.PROJECTNUMBER, "Project number"),
            x => (x.PROJECTNAME, "Sub-project number"),
            x => (x.ProjectManager, "Project manager"),
            x => (x.ProjectManager2, "Project manager 2")
            ); 

I am getting an exception:

System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object.
Source=DeliveryProjectAnalyzer StackTrace: at DeliveryProjectAnalyzer.Program.<>c.b__9_7(<>f__AnonymousType5`4 x) in x was null.

Why so? How x is null if it is printed to the console? I also don't understand at what point I should check for null as printing to Console works without any problems? In addition to that I have used CreateHTMLTable method before as well in similar cases for modifying List to HTML table without any issues.

c#
asked on Stack Overflow Sep 17, 2020 by hatman

1 Answer

0

For anyone who might be wondering the same, here is the solution:

    var ComparedHTMLTableProject = CreateHTMLTable,
        x => (x?.PROJECTNUMBER, "Project number"),
        x => (x?.PROJECTNAME, "Sub-project number"),
        x => (x?.ProjectManager, "Project manager"),
        x => (x?.ProjectManager2, "Project manager 2")
        ); 
answered on Stack Overflow Sep 17, 2020 by hatman

User contributions licensed under CC BY-SA 3.0