【发布时间】:2014-08-17 03:09:03
【问题描述】:
我有一个需要后台工作人员完成的例程。如下;
private void BatteryListFetchBackgroundWorkerRunWorkerCompleter(object sender, RunWorkerCompletedEventArgs e)
{
this.Cursor = Cursors.Default;
var sortedList = this.currentBatteries.Values.OrderBy(g => g, new BatteryNameComparer()); //breaks here
this.BatteryBindingSource.DataSource = sortedList;
if (this.batteryListBox.Items.Count > 0)
{
this.batteryListBox.SetSelected(0, true);
}
this.viewScheduleButton.Enabled = true;
this.viewDefaultScheduleButton.Enabled = true;
this.viewEditScheduleLimits.Enabled = true;
}
它中断的线是;
this.BatteryBindingSource.DataSource = sortedList;
该异常为空引用异常,在设置数据源时发生
BatteryNameComparer的代码
public class BatteryNameComparer : IComparer<Battery>
{
/// <summary>
/// Compares DDSMGroup
/// </summary>
/// <param name="a">first value for comparison</param>
/// <param name="b">second value for comparison</param>
/// <returns>An integer indicating the compare result</returns>
public int Compare(Battery a, Battery b)
{
int aId = int.Parse(a.DeviceName.Substring(BatteryOverviewControl.BatteryPrefixSubstring.Length));
int bId = int.Parse(b.DeviceName.Substring(BatteryOverviewControl.BatteryPrefixSubstring.Length));
return aId.CompareTo(bId);
}
}
【问题讨论】:
-
异常应该发生在比较器中,你能发布BatteryNameComparer的来源吗?
-
它已发布,但是当我在电池名称比较器中设置断点时,它从未到达
-
在处理异常时总是发布堆栈跟踪。
标签: c# sorting exception nullreferenceexception targetinvocationexception