1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C# index outside bounds of array

Discussion in 'C, C++, C#' started by p3nguin911, Mar 30, 2014.

  1. p3nguin911

    p3nguin911 Newbie

    Joined:
    Jan 5, 2013
    Messages:
    22
    Likes Received:
    3
    Im making a game and after the first run run when the user clicks new game I get index outside bounds of array.
    This is the the new game button code

    Code:
    panel1.Controls.Clear();
                      
                int x = int.Parse(heightentered.Text);
                int y = int.Parse(widthentered.Text);
                int density = int.Parse(blockedpercentageentered.Text); 
                uniwidth = y - 1;
                uniheight = x - 1;
                // 2d string array. Feel free to edit
                positions = new Label[y, x];
                empty = new int[y, x];
                           
                for (int i = 0; i < y; i++)
                {
                    for (int j = 0; j < x; j++)
                    {
                        Label gridmaze = new Label();
                        gridmaze.Size = new Size(8, 8);
                        gridmaze.Location = new Point(20 + (12 * i), 120 + (12 * j));
                        
                        gridmaze.Tag = new Random(Guid.NewGuid().GetHashCode()).Next(0, 100);
                        if (int.Parse(gridmaze.Tag.ToString()) < density)
                        {
                            
                            gridmaze.BackColor = Color.Red;
                            empty[i, j] = 1;
                        }
                        else
                        {
                            gridmaze.BackColor = Color.Yellow;
                            empty[i, j] = 0;
                        }
                        positions[i, j] = gridmaze;
                       
                        panel1.Controls.Add(gridmaze);
                    }
                }
                
                //set the borders to red
                for (int i = 0; i < y; i++)
                {
                    positions[i, 0].BackColor = Color.Red;
                    positions[i, x - 1].BackColor = Color.Red;
                    empty[i, 0] = 1;
                    empty[i, x - 1] = 1;
    
    
                }
                for (int i = 0; i < x; i++)
                {
                    positions[y - 1, i].BackColor = Color.Red;
                    positions[0, i].BackColor = Color.Red;
                    empty[0, i] = 1;
                    empty[y - 1, i] = 1;
                }
    
    
                //creates a start and end point
                 positions[1,5].BackColor = positions[2, 5].BackColor
                = positions[3, 5].BackColor = Color.Yellow;
                 empty[0,5] = empty[uniwidth,4] = empty[1, 5] = empty[2, 5] = empty[3,5] = 0;
                 positions[uniwidth - 2, 4].BackColor = positions[uniwidth - 1, 4].BackColor = positions[uniwidth, 4].BackColor = Color.Yellow;
                positions[0, 5].BackColor = positions[uniwidth, 5].BackColor = Color.Violet;


    ​I try to call a clear function that sets both my empty, and positions arrays to null but I still get the same error. Im not sure why this happens because even if it wasn't empty would it now just override whats already in there?
    Any help would be appreciated.

     
  2. Chris22

    Chris22 Regular Member

    Joined:
    Sep 29, 2010
    Messages:
    400
    Likes Received:
    1,059
    Your debugger should tell you what's causing it
     
  3. innosoft

    innosoft Jr. VIP Jr. VIP Premium Member

    Joined:
    Nov 25, 2008
    Messages:
    1,633
    Likes Received:
    639
    Occupation:
    Software Developer, SEO
    Location:
    Office
    Home Page:
    its most common one.. ur trying to access a position in array which actually doesnt exists in memory...
     
  4. Jeff Bezos

    Jeff Bezos Junior Member

    Joined:
    Jan 11, 2014
    Messages:
    178
    Likes Received:
    81
    Like innosoft said. Index out of bounds literally means your accessing an index in the array that doesn't exist. Its like having an array that has 10 slots and your trying to access the 11th slot, it doesn't exist.
     
  5. innosoft

    innosoft Jr. VIP Jr. VIP Premium Member

    Joined:
    Nov 25, 2008
    Messages:
    1,633
    Likes Received:
    639
    Occupation:
    Software Developer, SEO
    Location:
    Office
    Home Page:
    or might be memory is not initialized..
     
  6. Xp3r7

    Xp3r7 Regular Member

    Joined:
    Sep 9, 2011
    Messages:
    286
    Likes Received:
    386
    Occupation:
    Freelancer
    Location:
    In the middle of nowhere
    It seems to work here:

    [​IMG]

    Does that button call some other methods on click?
    Just post the whole method where the error occurs plus the exact line.
     
  7. THUNDERELVI

    THUNDERELVI Elite Member

    Joined:
    Sep 12, 2009
    Messages:
    2,202
    Likes Received:
    1,731
    Gender:
    Male
    Location:
    W3
    Debug the software and see in which line of code the error is being caused.
     
  8. p3nguin911

    p3nguin911 Newbie

    Joined:
    Jan 5, 2013
    Messages:
    22
    Likes Received:
    3
    Thanks turns out it has to do with some other functions that the problem is coming from. I have 2 questions.
    1. That functions is not called until the user clicks another button, so does the compiler not care about when its called as long as it's called?
    2. Is there a way to reset my entire function that I posted above so it doesn't run into this problem because it seems the problem is in multiple functions
     
  9. p3nguin911

    p3nguin911 Newbie

    Joined:
    Jan 5, 2013
    Messages:
    22
    Likes Received:
    3
    Can anyone help?
     
  10. xxf8xx

    xxf8xx Supreme Member

    Joined:
    Nov 30, 2009
    Messages:
    1,321
    Likes Received:
    596
    Occupation:
    IM
    I know this thread's pretty old, but if you can post the other code then that will help us figure it out. It might be as simple as adding a minus 1 somewhere. So far the code above runs fine without error, so you will have to post the rest of the code. And to answer your question, the compiler can tell if you are going to try to access a nonexistent element before even running the program.
     
  11. s0ap

    s0ap Executive VIP Jr. VIP Premium Member

    Joined:
    Sep 23, 2008
    Messages:
    230
    Likes Received:
    810
    Occupation:
    :] guess
    Location:
    Congo/DRC
    X and Y are logically reversed in your code, Y should be height and X should be width. You seem to be consistent about your usage, but if you are passing these off somewhere that is expecting traditional (x,y) layout you may have a bounds error. You (or I) also seem somewhat confused about your boundary conditions. Typically you want to avoid "val - 1" unless you are dealing with strings, as it is almost never needed.
     
    • Thanks Thanks x 1