0
它發生之前,我把我的詞典(詞典)改爲ConcurrentDictionary,並修復它。 但是,當我改變了事情,(字典保持不變)事情出錯了。甚至當ConcAddDictionary拋出NullReferenceException時TryAdd()
這裏是我的全部代碼: http://pasted.co/fd94e190
它說列表爲空。這就是方法。
Models.GridData getCurrentGrid()
{
Models.GridData data = new Models.GridData();
for (int i = 0; i < metroGrid1.Rows.Count; i++)
{
foreach (var item in metroGrid1.Rows[i].Cells)
{
if (item is DataGridViewCell)
{
Models.CellData cellData = new Models.CellData();
DataGridViewCell cell = item as DataGridViewCell;
cellData.column = cell.ColumnIndex;
cellData.row = cell.RowIndex;
if (string.IsNullOrEmpty((string)cell.Value) && string.IsNullOrWhiteSpace((string)cell.Value))
{
cellData.value = string.Empty;
}
else {
cellData.value = cell.Value;
}
//Occurs here right now
data.List.TryAdd(cellData.column.ToString() + " " + cellData.row.ToString(),cellData);
}
}
}
return data;
}
void Save()
{
Models.GridData data = new Models.GridData();
data = getCurrentGrid();
unsavedChanges = false;
StreamWriter writer = new StreamWriter(path);
writer.AutoFlush = true;
writer.Write(JsonConvert.SerializeObject(data.List));
writer.Close();
MetroFramework.MetroMessageBox.Show(this, "Başarıyla kaydedildi", "Yoklama Listesi-Talha Açıkgöz", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
}
編輯:Models.cs = http://pasted.co/13a7cb1c
而不是鏈接到長碼別處,請嘗試重現這在[MCVE] - 理想控制檯通過硬編碼數據的應用程序。在很多情況下,將真實代碼減少到最低程度的例子揭示了這個問題。 –
@JonSkeet謝謝,我會試試,但現在我的時間非常有限:\ –
在'Save'方法中,我沒有看到'data.List'的任何初始化,你似乎缺少一個。 – kiziu