1

enter image description hereenter image description hereenter image description hereenter image description here

I keep getting a System.NullReferenceException error when trying to add a model object to a list.

public class DataTablePrimaryKey
{
    public string ColumnName { get; set; }

    public string EpicColumnName { get; set; }

    public string PreviousValue { get; set; }

    public string EpicValue { get; set; }
}

Here is my Model for the Object that I am trying to add to the list

List<DataTablePrimaryKey> tbl2PKLinkTo1 = new List<DataTablePrimaryKey>();

Here I am creating the List for the model

DataTablePrimaryKey pkFind = new DataTablePrimaryKey();
//Replace the Tables if links are not null
if ((outputPrevTable2Link1 != null && outputPrevTable2Link1 != "") || primaryKeyEnteredTable1)
 {
     pkFind = tbl2PKLinkTo1.Find(
     delegate (DataTablePrimaryKey find)
     {
         return find.PreviousValue == outputPrevTable2Link1;
     });

    if (pkFind == null)
    {
        DataTablePrimaryKey primaryKeyModel = new DataTablePrimaryKey();

        primaryKeyModel.ColumnName = tbl2OldColumnName1;
        primaryKeyModel.EpicColumnName = tableInformation.Tbl2Table1Link;
        primaryKeyModel.EpicValue = outputTable2Link1;
        primaryKeyModel.PreviousValue = outputPrevTable2Link1;

        if (primaryKeyModel != null)
        {
            tbl2PKLinkTo1.Add(primaryKeyModel);
        }
    }
}

Here I am checking the list to find if the value has already been added to the list.

If the value has not been added to the list, then I want to create a new object called "PrimaryKeyModel" and I fill in the appropriate values and add it to the list

 tbl2PKLinkTo1.Add(primaryKeyModel);

This line of code is where I am getting the System Null Reference Error. I have inspected the model before it is added and all of the variables have values. I have no idea why this is happening.

jkdev
  • 11,360
  • 15
  • 54
  • 77
Eric
  • 212
  • 2
  • 15
  • 4
    `tbl2PKLinkTo1` is null ... – Trevor Sep 13 '19 at 14:44
  • How would it be null if I declared it as new List<> above it? – Eric Sep 13 '19 at 14:47
  • I don't know, but it's null. Set a breakpoint on that line and check the value of `tbl2PKLinkTo1` immediately before `.Add` is called. Also, you're not shadowing a class field variable named `tbl2PKLinkTo1`, are you? – ProgrammingLlama Sep 13 '19 at 14:48
  • The only other way this _might_ happen is if you're using multiple threads to add items to a single list. – ProgrammingLlama Sep 13 '19 at 14:51
  • I have put a breakpoint there and it is showing a count of 0 which means that it should't be null right? and no. – Eric Sep 13 '19 at 14:51
  • You might need to clean/rebuild your project. It might actually be a different line causing the exception. What does the stack trace say? – ProgrammingLlama Sep 13 '19 at 14:52
  • I will rebuild and attach some screen shots to the original question – Eric Sep 13 '19 at 14:54
  • Can you confirm by commenting out the tbl2PKLinkTo1.Add(primaryKeyModel) line? Since you are accessing a property of tableInformation object, do you also confirm it's not null? – codein Sep 13 '19 at 15:00
  • Where is this line in relation to all of the others? `List tbl2PKLinkTo1 = new List();` – Casey Crookston Sep 13 '19 at 15:02
  • Added two pictures to show the count of the list and the values for the model – Eric Sep 13 '19 at 15:04
  • Casey Crookston that line of code is the third line down from the start of the ActionResult. The code in the pictures is also inside this ActionResult. – Eric Sep 13 '19 at 15:05
  • well, yeah, this is odd. I dunno. Can you show us the entire stack trace maybe? – Casey Crookston Sep 13 '19 at 15:09
  • 1
    Also, just for fun, right before `tbl2PKLinkTo1.Add(primaryKeyModel);` try this: `int count = tbl2PKLinkTo1.Count;` – Casey Crookston Sep 13 '19 at 15:10
  • Yes I will add this into the original question as well, and I will try that as well – Eric Sep 13 '19 at 15:11
  • @CaseyCrookston I tried adding the int count = tbl2PkLinkTo1.Count() above that line and it skipped over it which is extremely odd, and I opened up the "Locals Window" in Visual Studio and searched for that variable and it is not found – Eric Sep 13 '19 at 15:22
  • Ok, you've got something funk going on. The code you are executing does not seem to be the code you are looking at in VS. Some things to try: Close VS. Find the associated `bin` and `obj` folders to this project and solution and delete all of them. Re-open VS, do a clean and re-build. Then try again. – Casey Crookston Sep 13 '19 at 15:30
  • did that help? :) – Casey Crookston Sep 13 '19 at 15:49
  • Sorry about that just got done deleting the files, cleaning, and rebuilding but still same error! – Eric Sep 13 '19 at 15:54
  • Does it still skip over the `int count = tbl2PKLinkTo1.Count;`? – Casey Crookston Sep 13 '19 at 15:57
  • Yes still skipping over the int count line of code! @CaseyCrookston – Eric Sep 13 '19 at 16:08
  • 1
    @CaseyCrookston I figured it out won't let me post the answer because it is marked as a duplicate, but I can explain it here. So it wasn't really breaking on that line of code. It was breaking on the line below it. I am using "IDbCommand" in this Action Result and it looks like it messes up the debugging experience.I had to use the "Count" before the List.Find because that was breaking it. Also below it I was trying to access a property from pkFind when it was null. Thank you for your time and effort! – Eric Sep 13 '19 at 17:16

0 Answers0