C#a在对象列表中有一个列表。

C# a having a list inside a list of objects. Will not allow .add

我一直在犯这个错误。

An unhandled exception of type 'System.NullReferenceException' occurred in CustomerExcelreader.exe

其他信息:

Object reference not set to an instance of an object.

我不知道为什么,因为我有一个全局对象,而且我知道对象可以工作,因为当我使用对象的非列表值时,一切都可以正常工作。

这是实际存在问题的对象。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CustomerExcelreader
{
public class customers
{
    public customers()
    {

    }
    /// <summary>
    /// These work just fine.
    /// </summary>
    public String Guest { get; set; }
    public String Contact { get; set; }
    public String ClubNumber { get; set; }//GuestNumber
    public String CasioNumber { get; set; }

    /// <summary>
    /// Lists of lists
    /// </summary>
    public List<String> Dates { get; set; }
    public List<String> Description { get; set; }
    public List<Double> moneyIN { get; set; }
    public List<Double> moneyOUT { get; set; }
    public List<Double> Balance { get; set; }
    public List<String> Remarks { get; set; }

}
}

这里是它与任何列表值混淆的地方,但是正常变量是可以的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
private void addButton_Click(object sender, EventArgs e)
{
    //customers add_customerHistory;
    if (listNames.SelectedIndex >= 0)// && listNames.SelectedIndex <= Pathlist.Capacity)//somehow SelectedIndex could be less then 0 by not picking something.
    {
        //add_customerHistory = CustomerHistoryList[listNames.SelectedIndex];
        textBox1.Text = CustomerHistoryList[listNames.SelectedIndex].ClubNumber;
        CustomerHistoryList[listNames.SelectedIndex].Dates.Add(DateBox.Text);
        CustomerHistoryList[listNames.SelectedIndex].Description.Add(DescriptionBox.Text);
        CustomerHistoryList[listNames.SelectedIndex].moneyIN.Add(Convert.ToDouble(InBox.Text));
        CustomerHistoryList[listNames.SelectedIndex].moneyOUT.Add(Convert.ToDouble(OutBox.Text));
        CustomerHistoryList[listNames.SelectedIndex].Balance.Add(Convert.ToDouble(BalanceBox.Text));
        CustomerHistoryList[listNames.SelectedIndex].Remarks.Add(RemarkBox.Text);
        //CustomerHistoryList[listNames.SelectedIndex].CasioNumber ="Its been changed";
        richTextBox1.Text = CustomerHistoryList[listNames.SelectedIndex].CasioNumber;

    }
    else
    {
        CustomerHistoryList[0].Dates.Add(DateBox.Text);
        CustomerHistoryList[0].Description.Add(DescriptionBox.Text);
        CustomerHistoryList[0].moneyIN.Add(Convert.ToDouble(InBox.Text));
        CustomerHistoryList[0].moneyOUT.Add(Convert.ToDouble(OutBox.Text));
        CustomerHistoryList[0].Balance.Add(Convert.ToDouble(BalanceBox.Text));
        CustomerHistoryList[0].Remarks.Add(RemarkBox.Text);
    }

}

它在这里不起作用似乎很奇怪。我想这和物体内部的列表有关吧?我听说那些东西会很奇怪。