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.