none
Error message during debugging

    Question

  • Hello,

    I am a new born to MS VS. :) 

    Recently, I have been given a code which is written by someone else. I tried to open debugged it using VS 2015. However, an error message keeps popping up. 

    ====

    System.IndexOutOfRangeException was unhandled
    Message: An unhandled exception of type 'System.IndexOutOfRangeException' occurred in SerialPort Terminal.exe
    Additional information: Index was outside the bounds of the array.

    ===

    I then open the .exe file generated from this code and the same error appears:

    'Unhandled exception has occurred in your application. If you click Continue, the application will ignore this error and attempt to continue. If you click Quit, the application will close immediately. Index was outside the bounds of the array.'

    My question is which array does this message mean? The person who wrote this hasn't had any problem like this and I just copied his entire code to my PC. The only thing i can think of is that this programme grabs image files from a certain folder of which the path on my PC might be different than that of on his PC. 

    Any help are appreciated.

    Rock

    Thursday, April 07, 2016 8:47 AM

Answers

  • Hi Rock,

    thank you for providing more details.

    Regarding the message "You are debugging a Release build" - in Visual Studio you can set what build you want to get. One way is to use the "Build" Toolbar (View -> Toolbar -> Build could be used to show it if it is not displayed). You get a small toolbar with dropdowns where you should be able to select between Debug and Release. So maybe you want to switch to Debug.

    One thing is see in your code are your for loops:

    for (int i = 0; i < stg.trainingTrials; i+=8)

    So you increase by 8 after each loop execution and inside the loop you access i to i+7.

    But i+7 might be equal or greater than stg.trainingTrials so you might get a problem with your array. So please check the value of stg.trainingTrials if that might cause the trouble.

    Example: stg.trainingTrials is 4.
    Now the loop starts. i=0, 0 < 4: yes. So we access the elements 0..7 but the array only got 4 elements (0..3) so when you access array element 4 you get an exception.

    Possible solutions:
    a) check for int i=0, i<stg.trainingTrails-7, i+=8)
    -> Now it makes sure that you have the elements that you access. So with the example on top: 0 < 4-7 - so the loop is not executed.

    b) A better approach might be (if the value of stg.trainingTrails is user given): Check that the value can be divided by 8. So if (stg.trainingTrials % 8 != 0) -> Error! Ask user to correct value or tell him that the value was not ok and that you work with stg.trainingTrials / 8 * 8 instead. 

    With kind regards,

    Konrad

    Friday, April 08, 2016 10:59 AM
    Moderator

All replies

  • Hi Rock1997,

    This error occurs if you try to access an element in the that is out side of its range. Suppose your array has 5 elements so if you try access the element 6 then you will get this error because it is 6 is outside of range of the range. I guess in your code a for loop might be trying to access a value in the array that is outside of its range. For more info please see the below articles. Hope this helps you.

    IndexOutOfRangeException Class
    IndexOutOfRangeException 

    Thanks,

    Sabah Shariq

    Thursday, April 07, 2016 9:35 AM
  • Hi,

    I understood that you got the source of the application so you could start it from Visual Studio in Debug Mode.

    I would suggest to do these steps:
    a) Press Ctrl - Alt - E - that should open a window in which you could select when the debugger should interrupt when an Exception is thrown ("Break When Thrown"). I would recommend to select all Common Language Runtime Exceptions but at least you should make sure that System.IndexOutOfRangeException is selected.

    b) When you start the application through Debug -> Start Debugging (or pressing F5), then the Debugger should stop as soon as this exception is thrown. So you should be able to inspect the place where it occured. 

    Instead of these steps, you might want to set some breakpoints and then stept through the code after hitting a breakpoint. That might be much more work than setting the Debugger to break execution as soon as an Exception is thrown. (This will also break the execution, when the exception is handled so a Debugger stopping must not be a sign of an error!)

    I hope this was of some help for you to find the issue. 

    With kind regards,

    Konrad

    Thursday, April 07, 2016 9:54 AM
    Moderator
  • Hi Konrad,

    I have followed your steps to identify what happens and received the following message "Debugger: Stopped at Exception:frmStimulus".I have checked the definition of "frmStimulus" but cannot find anything odd. Here are the section of the code which involves 'frmStimulus':

    =======                    

        public partial class frmStimulus : Form
        {
            //private string receiver;
            System.Timers.Timer timer = new System.Timers.Timer();
            private readonly frmTerminal _frmTerminal;

            private bool show_stimulus;

            private bool show_bottons = false;
            private ExperimentSettings settings;

            private string[] positive_stimuli = { "P007", "P013", "P026", "P030", "P033", "P035", "P039", "P045", "P053", "P067", "P070", "P071", "P085", "P091", "P092", "P098", "P101", "P114", "P117", "P130" };
            private string[] negative_stimuli_snake = { "Sn016", "Sn048", "Sn124", "Sn125", "Sn131"};
            private string[] negative_stimuli_spider = { "Sp099", "Sp124", "Sp127", "Sp136", "Sp153"};
            private string[] negative_stimuli_human = { "H034", "H056", "H066", "H100", "H122"};
            private string[] negative_stimuli_animal = { "A001", "A013", "A041", "A075", "A100"};
            private int training_trial_counter;
            private int testing_trial_counter;
            private string[] training_stimuli;
            private string[] testing_stimuli;

            private int[] random_training_list;
            private int[] random_testing_list;


           // private int max_training_trials, max_testing_trials;

            private bool training_mode, testing_mode;


            static DateTime file_name_time = DateTime.Now;
            StreamWriter log_file = new StreamWriter("eeg_user_rating-" + file_name_time.ToString("dd-MMM-yyyy-HH-mm") + ".txt");


            public frmStimulus(frmTerminal Terminal, ExperimentSettings stg)
            {
                this._frmTerminal = Terminal;
               // max_testing_trials = stg.testingTrials;
                //max_training_trials = stg.trainingTrials;

                settings = stg;
                InitializeComponent();

                btnPositive.Visible = false;
                btnNegative.Visible = false;
                lblComplited.Visible = false;
                lblThanks.Visible = false;
                //btnNeutral.Visible = false;

                show_stimulus = false;
                showPrestimulus();

                //create training and testing stimuli
                training_stimuli = new string[stg.trainingTrials];
                testing_stimuli = new string[stg.testingTrials];

                //populate training stimuli
                Random rnd_training = new Random();
                for (int i = 0; i < stg.trainingTrials; i+=8)
                {
                    training_stimuli[i] = @"Positive\" + get_random_picture(training_stimuli, positive_stimuli,0, 19) + ".bmp";
                    training_stimuli[i+1] = @"Negative\Animals\" + get_random_picture(training_stimuli, negative_stimuli_animal, 0, 4) + ".bmp";
                    training_stimuli[i+2] = @"Positive\" + get_random_picture(training_stimuli,positive_stimuli,0, 19) + ".bmp";
                    training_stimuli[i+3] = @"Negative\Human\" + get_random_picture(training_stimuli, negative_stimuli_human, 0, 4) + ".bmp";
                    training_stimuli[i+4] = @"Positive\" + get_random_picture(training_stimuli,positive_stimuli,0, 19) + ".bmp";
                    training_stimuli[i+5] = @"Negative\Snakes\" + get_random_picture(training_stimuli,negative_stimuli_snake, 0, 4) + ".bmp";
                    training_stimuli[i+6] = @"Positive\" + get_random_picture(training_stimuli, positive_stimuli, 0, 19) + ".bmp";
                    training_stimuli[i+7] = @"Negative\Spiders\" + get_random_picture(training_stimuli, negative_stimuli_spider,0, 4) + ".bmp";

                }

                //populate testing trials
                Random rnd_testing = new Random();

                for (int i = 0; i < stg.testingTrials; i += 8)
                {
                    testing_stimuli[i] = @"Positive\" + get_random_picture(training_stimuli, testing_stimuli,positive_stimuli, 0, 19) + ".bmp";
                    testing_stimuli[i + 1] = @"Negative\Animals\" + get_random_picture(training_stimuli, testing_stimuli,negative_stimuli_animal, 0, 4) + ".bmp";
                    testing_stimuli[i + 2] = @"Positive\" + get_random_picture(training_stimuli, testing_stimuli,positive_stimuli, 0, 19) + ".bmp";
                    testing_stimuli[i + 3] = @"Negative\Human\" + get_random_picture(training_stimuli, testing_stimuli,negative_stimuli_human, 0, 4) + ".bmp";
                    testing_stimuli[i + 4] = @"Positive\" + get_random_picture(training_stimuli, testing_stimuli,positive_stimuli, 0, 19) + ".bmp";
                    testing_stimuli[i + 5] = @"Negative\Snakes\" + get_random_picture(training_stimuli, testing_stimuli,negative_stimuli_snake, 0, 4) + ".bmp";
                    testing_stimuli[i + 6] = @"Positive\" + get_random_picture(training_stimuli, testing_stimuli,positive_stimuli, 0, 19) + ".bmp";
                    testing_stimuli[i + 7] = @"Negative\Spiders\" + get_random_picture(training_stimuli, testing_stimuli,negative_stimuli_spider, 0, 4) + ".bmp";

                }

                //randomize the list
                Random ran = new Random();
                random_testing_list = Enumerable.Range(0,testing_stimuli.Length).OrderBy(x => ran.Next()).ToArray();
                random_training_list = Enumerable.Range(0, training_stimuli.Length).OrderBy(x => ran.Next()).ToArray();

                //initlize
                training_mode = true;
                testing_mode = false;
                testing_trial_counter = 0;
                training_trial_counter = 0;
            }

     ===========

    In addition to above, I also received a warning message before debugging:

    "You are debugging a Release build of SerialPort Terminal.exe. Using Just My Code with Release builds using complier optimizations results in a degraded debugging experience (e.g. breakpoints will not be hit)."

    Thanks,

    Rock
    Friday, April 08, 2016 10:37 AM
  • Hi Rock,

    thank you for providing more details.

    Regarding the message "You are debugging a Release build" - in Visual Studio you can set what build you want to get. One way is to use the "Build" Toolbar (View -> Toolbar -> Build could be used to show it if it is not displayed). You get a small toolbar with dropdowns where you should be able to select between Debug and Release. So maybe you want to switch to Debug.

    One thing is see in your code are your for loops:

    for (int i = 0; i < stg.trainingTrials; i+=8)

    So you increase by 8 after each loop execution and inside the loop you access i to i+7.

    But i+7 might be equal or greater than stg.trainingTrials so you might get a problem with your array. So please check the value of stg.trainingTrials if that might cause the trouble.

    Example: stg.trainingTrials is 4.
    Now the loop starts. i=0, 0 < 4: yes. So we access the elements 0..7 but the array only got 4 elements (0..3) so when you access array element 4 you get an exception.

    Possible solutions:
    a) check for int i=0, i<stg.trainingTrails-7, i+=8)
    -> Now it makes sure that you have the elements that you access. So with the example on top: 0 < 4-7 - so the loop is not executed.

    b) A better approach might be (if the value of stg.trainingTrails is user given): Check that the value can be divided by 8. So if (stg.trainingTrials % 8 != 0) -> Error! Ask user to correct value or tell him that the value was not ok and that you work with stg.trainingTrials / 8 * 8 instead. 

    With kind regards,

    Konrad

    Friday, April 08, 2016 10:59 AM
    Moderator