DataTable索引超出范围异常处理,仍然错误抛出

时间:2016-01-25 08:57:24

标签: c# datatables

我在运行时遇到一个奇怪的错误。我说奇怪,因为我已经处理了可能引发错误的异常(至少在我看来)。请详细说明这个话题。我试图找到答案,但我找不到这个具体问题的答案。任何人在标记它之前都会读取代码。下面是我得到的错误的快照 Error

我查看了我的代码,并特别确保使用DataTable.Rows.Count(对于supportPointSelected)不会发生这种情况。我检查Count,如果且仅仅是大于0(代码的第一个IF语句)我继续它。请找到以下代码

private List<byte> routeHandler(DataTable supportPointSelected, double taskState, int indices)
{
    //TPCANStatus statusCan = new TPCANStatus();
    int a = supportPointSelected.Columns.IndexOf("Number");     // column number indices
    int b = supportPointSelected.Columns.IndexOf("XSupport"); // column number X
    int c = supportPointSelected.Columns.IndexOf("YSupport"); // column number Y
    int d = supportPointSelected.Columns.IndexOf("VSupport"); // column number Velocity
    int v11, v22;
    byte i1, i2,
        x1, x2, x3, x4,
        y1, y2, y3, y4,
        v1, v2;
    if (supportPointSelected.Rows.Count > 0) // Check to Avoid the Error
    {
        if (Convert.ToDouble(supportPointSelected.Rows[0][d]) == 0)
        {
            supportPointSelected.Rows[0][d] = 0.01; // This is the Point the Error Occurs
        }
        else { }
        if ((taskState == 1 ) || (taskState ==2))
        {
            if (indices > 0)
            {
                // Check for Index overflow. 
                if (indices > supportPointSelected.Rows.Count)
                    indices = supportPointSelected.Rows.Count;
                else
                { }
                // Port Index into Bytes
                i1 = Convert.ToByte(Convert.ToInt16(supportPointSelected.Rows[indices - 1][a]) & 0x00FF);
                i2 = Convert.ToByte((Convert.ToInt16(supportPointSelected.Rows[indices - 1][a]) & 0xFF00) >> 8);
                // Port X into Bytes
                x1 = Convert.ToByte(Convert.ToInt32(Convert.ToDouble(supportPointSelected.Rows[indices - 1][b]) * 100) & 0x00FF);
                x2 = Convert.ToByte((Convert.ToInt32(Convert.ToDouble(supportPointSelected.Rows[indices - 1][b]) * 100) & 0xFF00) >> 8);
                x3 = Convert.ToByte((Convert.ToInt32(Convert.ToDouble(supportPointSelected.Rows[indices - 1][b]) * 100) & 0xFF0000) >> 16);
                x4 = Convert.ToByte((Convert.ToInt32(Convert.ToDouble(supportPointSelected.Rows[indices - 1][b]) * 100) & 0xFF000000) >> 24);
                // Port Y into Bytes
                y1 = Convert.ToByte(Convert.ToInt32(Convert.ToDouble(supportPointSelected.Rows[indices - 1][c]) * 100) & 0x00FF);
                y2 = Convert.ToByte((Convert.ToInt32(Convert.ToDouble(supportPointSelected.Rows[indices - 1][c]) * 100) & 0xFF00) >> 8);
                y3 = Convert.ToByte((Convert.ToInt32(Convert.ToDouble(supportPointSelected.Rows[indices - 1][c]) * 100) & 0xFF0000) >> 16);
                y4 = Convert.ToByte((Convert.ToInt32(Convert.ToDouble(supportPointSelected.Rows[indices - 1][c]) * 100) & 0xFF000000) >> 24);
                // Port Velocity into Bytes
                v11 = Convert.ToInt32(Convert.ToDouble(supportPointSelected.Rows[indices - 1][d]) * 100);
                v1 = Convert.ToByte(v11 & 0x00FF);
                v22 = (Convert.ToInt32(Convert.ToDouble(supportPointSelected.Rows[indices - 1][d]) * 100) & 0xFF00);
                v2 = Convert.ToByte((v22 & 0xFF00) >> 8);
            }
            else
            {
                i1 = 1;
                i2 = 0;
                x1 = 0;
                x2 = 0;
                x3 = 0;
                x4 = 0;
                y1 = 0;
                y2 = 0;
                y3 = 0;
                y4 = 0;
                v1 = 0;
                v2 = 0;
            }
        }

        else
        {
            // Porting Index into Bytes
            i1 = 0;
            i2 = 0;
            // Porting X into Bytes
            x1 = 0;
            x2 = 0;
            x3 = 0;
            x4 = 0;
            // Porting Y into Bytes
            y1 = 0;
            y2 = 0;
            y3 = 0;
            y4 = 0;
            // Porting Velocity into Bytes
            v1 = 0;
            v2 = 0;
        }
    }
    else {
        i1 = 0;
        i2 = 0;
        x1 = 0; x2 = 0; x3 = 0; x4 = 0;
        y1 = 0; y2 = 0; y3 = 0; y4 = 0;
        v1 = 0; v2 = 0;                 
    }
    List<byte> output = new List<byte>();
    output.Add(i1);
    output.Add(i2);
    output.Add(x1);
    output.Add(x2);
    output.Add(x3);
    output.Add(x4);
    output.Add(y1);
    output.Add(y2);
    output.Add(y3);
    output.Add(y4);
    output.Add(v1);
    output.Add(v2);
    return output;
}

1 个答案:

答案 0 :(得分:1)

在您的方法执行时,supportPointSelected是固定的,还是可能从另一个线程更改它?发生异常时,哪个值不正确?是不正确还是没有第0行?你能修复列索引并尝试以这种方式调试吗?我知道这不是一个真正的答案,但缺少太多的东西。