0

I have a routine which is called upon a background worker completing. As below;

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;
}

The line it's breaking at is;

                this.BatteryBindingSource.DataSource = sortedList;

The exception is null reference exception and it occurs when setting the data source

The code for 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);
    }
}
4

1 回答 1

0

每当你看到 aTargetInvocationException时,你应该立即检查它的InnerException属性以找到实际抛出的异常。

http://msdn.microsoft.com/en-us/library/system.exception.innerexception(v=vs.110).aspx

简单地说,aTargetInvocationException只告诉您在某种调用上下文中引发了另一个异常——这几乎总是一个跨线程操作或通过反射调用。

于 2014-06-26T11:03:30.717 回答