Abhishek Kumar
Abhishek Kumar

Reputation: 352

DataTable Index Out of Range Exception Handled, Still Error Thrown

I get a strange Error during Runtime. I say strange because I have handled the Exception which can possibly throw an Error (At least in my mind). Please shed some light on this topic.I have tried to find the answer but I could not find answer to this specific problem. Anyone before marking it a duplicate read the code. Below is a snapshot of Error I get Error

I looked into my code and I specifically make it sure that this doesn't happen by using DataTable.Rows.Count (for supportPointSelected). I check the Count and if and only of its lager than 0 (the First IF statement of the code) I go ahead with it. Please find the code below

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

Upvotes: 0

Views: 1195

Answers (1)

Jef Patat
Jef Patat

Reputation: 999

Is supportPointSelected fixed while your method is executing or might something be changing it from another thread? When the exception occurs, which value is incorrect? Is d incorrect or is there no row 0? Can you fix the column index and try to debug it that way? I know this isn't a real answer but too many pieces are missing.

Upvotes: 1

Related Questions