Nat*_*han 0 .net c# exception winforms visual-studio-2012
在我的程序中,我收到错误:
An unhandled exception of type 'System.NullReferenceException' occurred in POS System.exe
Additional information: Object reference not set to an instance of an object.
Run Code Online (Sandbox Code Playgroud)
当我尝试向TransactionList添加内容时会发生这种情况,如下所示.TransactionList是一个类实例列表,声明如下:
public static List<Transaction> TransactionList { get; set; }
这是Transaction类:
class Transaction
{
public double TotalEarned { get; set; }
public double TotalHST { get; set; }
public double TotalCost { get; set; }
public string Category { get; set; }
public int DaysSince2013 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
有什么线索在这里错了吗?我似乎无法找到为什么抛出这个错误...谢谢!
for (int i = 0; i < (lines / 5); i++)
{
TransactionList.Add(new Transaction //Error happens on this line
{
TotalEarned = Convert.ToDouble(stringArray[(i * 5)]),
TotalCost = Convert.ToDouble(stringArray[(i * 5) + 1]),
TotalHST = Convert.ToDouble(stringArray[(i * 5) + 2]),
Category = stringArray[(i * 5) + 3],
DaysSince2013 = Convert.ToInt32(stringArray[(i * 5) + 4])
});
}
Run Code Online (Sandbox Code Playgroud)
只是在你之前初始化它for loop
?
if (TransactionList == null)
TransactionList = new List<Transaction>();
for (int i = 0; i < (lines / 5); i++)
{
TransactionList.Add(new Transaction //Error happens on this line
{
TotalEarned = Convert.ToDouble(stringArray[(i * 5)]),
TotalCost = Convert.ToDouble(stringArray[(i * 5) + 1]),
TotalHST = Convert.ToDouble(stringArray[(i * 5) + 2]),
Category = stringArray[(i * 5) + 3],
DaysSince2013 = Convert.ToInt32(stringArray[(i * 5) + 4])
});
}
Run Code Online (Sandbox Code Playgroud)
或者,如果你不喜欢它,既然你已经宣布它static
,你可以这样做:
public static List<Transaction> TransactionList = new List<TransactionList>();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
265 次 |
最近记录: |