winforms .net 6 -对象引用未设置为对象的示例-对象示例化后,数据有效

w51jfk4q  于 2023-05-01  发布在  .NET
关注(0)|答案(1)|浏览(142)

现在,在有人开枪打我之前-我知道这已经被问了很多。我已经通读过了,但还是不太明白发生了什么。我有一个Windows窗体应用程序,使用。net 6.我已经创建了两个模型,其中将包含付款细节的列表,然后第二个模型将包含付款细节的列表,沿着其他两个项目。
我大约在一年前写了这篇文章,当时我正在学习C#,并且对C#的理解有所提高(现在使用模型,而我以前没有模型)。可能是Populate函数中的一个错误导致了这个错误,我不知道。代码的反馈总是赞赏。
Populate函数接受一个字符串列表,这些字符串以逗号分隔。有一个整数数组传入,这将表示在逗号分隔的对象中,所需值的正确位置,然后将其分配给相应的变量。
这两个模型是:

public class PaymentData
{
    public string SortCode { get; set; }
    public string AccountNumber { get; set; }
    public string PayeeName { get; set; }
    public Decimal Amount { get; set; }
    public string PayeeReference { get; set; }
}
public class PaymentList
{
    public List<PaymentData> PaymentData { get; set; }
    public Decimal Amount { get; set; } = new Decimal(0);
    public int NumberOfRows { get; set; } = 0;
}

在我的代码中,它们被示例化:

public static PaymentList Populate(List<string> ArrayData, int[] DataPosition)
    {
        // Create a new PaymentList object
        PaymentList paymentList = new PaymentList();  

        // Let's check if any of the DataPosition elements return -1 (is not set)
        if (DataPosition[0] != -1 &&
            DataPosition[1] != -1 &&
            DataPosition[2] != -1 &&
            DataPosition[3] != -1)
        {
            // Loop through each line within ArrayData
            foreach (string lineArr in ArrayData)
            {
                if (lineArr.Length > 4)
                {
                    string[] line = lineArr.Split(",");                        

                    // Get the Sort Code from the array
                    string SortCodeString = StringReplace.clean(line[DataPosition[0]].ToString().Trim());

                    if (SortCodeString.Contains("/"))
                    {
                        (string SortCode, bool Status) = SortCodeCheckIsDate.DateCheck(SortCodeString);
                        if (Status)
                        {
                            SortCodeString = SortCode;
                        }
                        else
                        {
                            DisplayMessage.MessageShow("Sort Code", SortCodeString);
                        }
                    }

                    // Check that the Sort Code is 6 digits, and only numeric
                    bool CheckSortCodeIsInt = int.TryParse(SortCodeString, out int SortCodeOut);
                    if (SortCodeString.Length != 6 | !CheckSortCodeIsInt)
                    {
                        DisplayMessage.MessageShow("Sort Code", SortCodeString);
                        break;
                    }

                    // Check that the Account Number is 8 digits, and only numeric
                    string AccountNoString = line[DataPosition[1]].ToString().PadLeft(8, '0').Trim();

                    bool CheckAccountNoIsInt = int.TryParse(AccountNoString, out int AccountNoOut);

                    if (AccountNoString.Length != 8 | !CheckAccountNoIsInt)
                    {
                        DisplayMessage.MessageShow("Account Number", AccountNoString);
                        break;
                    }

                    // Check Employee Name string length greater than zero
                    string EmployeeNameString = line[DataPosition[2]].ToString().Trim();
                    if (EmployeeNameString.Length == 0)
                    {
                        DisplayMessage.MessageShow("Employee Name", null);
                        break;
                    }

                    // Check that the Amount is a decimal place value
                    string AmountString = line[DataPosition[3]].ToString();
                    bool CheckAmountIsDecimal = decimal.TryParse(AmountString, out decimal AmountOut);
                    if (CheckAmountIsDecimal || AmountString.Contains("."))
                    {
                        // Sum the total Amount to display in the UI
                        paymentList.Amount += decimal.Parse(AmountString);
                    }
                    else
                    {
                        DisplayMessage.MessageShow("Amount", AmountString);
                        break;
                    }

                    // Set PayRefString from the input box paymentRef                        
                    string PayRefString = DataPosition[4] > -1 ? line[DataPosition[4]].ToString().Trim() : "None";

                    // Create a new row for DataTable and add data
                    PaymentData payment = new PaymentData
                    {                      
                        SortCode = SortCodeString.Trim(),
                        AccountNumber = AccountNoString.Trim(),
                        PayeeName = EmployeeNameString.Trim(),
                        Amount = AmountOut,
                        PayeeReference = PayRefString
                    };

                    // Create a new ListViewItem, assigned from the row variable
                    paymentList.PaymentData.Add(payment);

                    // Increment the NumRow variable by 1
                    paymentList.NumberOfRows++;
                }
            }                
        }
        return paymentList;
    }

错误发生在:

PaymentData payment = new PaymentData    
{                      
    SortCode = SortCodeString,
    AccountNumber = AccountNoString,
    PayeeName = EmployeeNameString,
    Amount = AmountOut,
    PayeeReference = PayRefString
};

我已经验证了SortCodeStringAccountNoStringEmployeeNameStringAmountOut(int)和PayRefString都是有效的。
填充PaymentData后,它将添加到PaymentList下的List中
我只是不明白,为什么当我尝试插入PaymentData的新示例时,它会给我错误Object reference not set to an instance of an object

qojgxg4l

qojgxg4l1#

@SvyatoslavDanyliv提供了正确的模型初始化和PaymentData列表。修改后的模型现在是:

public class PaymentList
    {
        public List<PaymentData> PaymentData { get; set; } = new List<PaymentData>();
        public Decimal Amount { get; set; } = new Decimal(0);
        public int NumberOfRows { get; set; } = 0;
    }

相关问题