index was outside the bounds of the array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nikz
    New Member
    • Feb 2012
    • 1

    index was outside the bounds of the array

    An error shows rarely iindex was outside the bounds of the array in line number 160





    Line number 160 starts below.
    Code:
             string s = "";
                    if (imgname[pageno - 1, 0] != null)
                    {
                        ImageButton1.Visible = true;
                        TextBox1.Visible = true;
                        ImageButton1.ImageUrl = image_url + imgname[pageno - 1, 0];
                        ImageButton1.AlternateText = imgname[pageno - 1, 0];
                        s = "<p><a href=\"http://www.status99.com/home.aspx\" target=\"_blank\"><img src=\" http://status99.com/main/greetings/" + type + "/" + imgname[pageno - 1, 0] + "\" alt=\"\" /> </a><br><a href=\"www.status99.com\"><br /><img src=\"http://www.status99.com/images/arrow.gif\" alt=\"\" /><br />status99.com , V4orkut.info(" + setcategory(type) + ") </a></p>";
                        TextBox1.Text = s;
                        //Literal1.Text = "<a name=\"fb_share\" type=\"box_count\" share_url=\"http://status99.com/main/greetings/" + type + "/" + imgname[pageno - 1, 0] + "\"></a>" +
                        //                       "<script src=\"http://static.ak.fbcdn.net/connect.php/js/FB.Share\" type=\"text/javascript\"></script>";
                        ImgButtonUFB_1.Visible = true;
                        divMessaegUFB_1.Visible = true;
    
    		 lblDesc1.Text = GetImgDesc(imgname[pageno - 1, 0]); 
    
                    }
                    if (imgname[pageno - 1, 1] != null)
                    {
                        ImageButton2.Visible = true;
                        TextBox2.Visible = true;
                        ImageButton2.ImageUrl = image_url + imgname[pageno - 1, 1];
                        ImageButton2.AlternateText = imgname[pageno - 1, 1];
                        s = "<p><a href=\"http://www.status99.com/home.aspx\" target=\"_blank\"><img src=\" http://status99.com/main/greetings/" + type + "/" + imgname[pageno - 1, 1] + "\" alt=\"\" /> </a><br><a href=\"www.status99.com\"><br /><img src=\"http://www.status99.com/images/arrow.gif\" alt=\"\" /><br />status99.com  V4orkut.info(" + setcategory(type) + ") </a></p>";
                        TextBox2.Text = s;
                        //Literal2.Text = "<a name=\"fb_share\" type=\"box_count\" share_url=\"http://status99.com/main/greetings/" + type + "/" + imgname[pageno - 1, 1] + "\"></a>" +
                        //                       "<script src=\"http://static.ak.fbcdn.net/connect.php/js/FB.Share\" type=\"text/javascript\"></script>";
                        ImgButtonUFB_2.Visible = true;
                        divMessaegUFB_2.Visible = true;
    
    		  lblDesc2.Text = GetImgDesc(imgname[pageno - 1, 1]); 
    
                    }
                    if (imgname[pageno - 1, 2] != null)
                    {
                        ImageButton3.Visible = true;
                        TextBox3.Visible = true;
                        ImageButton3.ImageUrl = image_url + imgname[pageno - 1, 2];
                        ImageButton3.AlternateText = imgname[pageno - 1, 2];
                        s = "<p><a href=\"http://www.status99.com/home.aspx\" target=\"_blank\"><img src=\" http://status99.com/main/greetings/" + type + "/" + imgname[pageno - 1, 2] + "\" alt=\"\" /> </a><br><a href=\"www.status99.com\"><br /><img src=\"http://www.status99.com/images/arrow.gif\" alt=\"\" /><br />status99.com V4orkut.info (" + setcategory(type) + ") </a></p>";
                        TextBox3.Text = s;
                        //Literal3.Text = "<a name=\"fb_share\" type=\"box_count\" share_url=\"http://status99.com/main/greetings/" + type + "/" + imgname[pageno - 1, 2] + "\"></a>" +
                        //                       "<script src=\"http://static.ak.fbcdn.net/connect.php/js/FB.Share\" type=\"text/javascript\"></script>";
                        ImgButtonUFB_3.Visible = true;
                        divMessaegUFB_3.Visible = true;
    	
    		lblDesc3.Text = GetImgDesc(imgname[pageno - 1, 2]); 
    
                    }
                    if (imgname[pageno - 1, 3] != null)
                    {
                        ImageButton4.Visible = true;
                        TextBox4.Visible = true;
                        ImageButton4.ImageUrl = image_url + imgname[pageno - 1, 3];
                        ImageButton4.AlternateText = imgname[pageno - 1, 3];
                        s = "<p><a href=\"http://www.status99.com/home.aspx\" target=\"_blank\"><img src=\" http://status99.com/main/greetings/" + type + "/" + imgname[pageno - 1, 3] + "\" alt=\"\" /> </a><br><a href=\"www.status99.com\"><br /><img src=\"http://www.status99.com/images/arrow.gif\" alt=\"\" /><br />status99.com V4orkut.info (" + setcategory(type) + ") </a></p>";
                        TextBox4.Text = s;
                        //Literal4.Text = "<a name=\"fb_share\" type=\"box_count\" share_url=\"http://status99.com/main/greetings/" + type + "/" + imgname[pageno - 1, 3] + "\"></a>" +
                        //                       "<script src=\"http://static.ak.fbcdn.net/connect.php/js/FB.Share\" type=\"text/javascript\"></script>";
    
                        ImgButtonUFB_4.Visible = true;
                        divMessaegUFB_4.Visible = true;
    		 lblDesc4.Text = GetImgDesc(imgname[pageno - 1, 3]); 
                    }
                    if (imgname[pageno - 1, 4] != null)
                    {
                        ImageButton5.Visible = true;
                        TextBox5.Visible = true;
                        ImageButton5.ImageUrl = image_url + imgname[pageno - 1, 4];
                        ImageButton5.AlternateText = imgname[pageno - 1, 4];
                        s = "<p><a href=\"http://www.status99.com/home.aspx\" target=\"_blank\"><img src=\" http://status99.com/main/greetings/" + type + "/" + imgname[pageno - 1, 4] + "\" alt=\"\" /> </a><br><a href=\"www.status99.com\"><br /><img src=\"http://www.status99.com/images/arrow.gif\" alt=\"\" /><br />status99.com (" + setcategory(type) + ") </a></p>";
                        TextBox5.Text = s;
                        //Literal5.Text = "<a name=\"fb_share\" type=\"box_count\" share_url=\"http://status99.com/main/greetings/" + type + "/" + imgname[pageno - 1, 4] + "\"></a>" +
                        //                       "<script src=\"http://static.ak.fbcdn.net/connect.php/js/FB.Share\" type=\"text/javascript\"></script>";
                        ImgButtonUFB_5.Visible = true;
                        divMessaegUFB_5.Visible = true;
    
    		 lblDesc5.Text = GetImgDesc(imgname[pageno - 1, 4]); 
                    }
                   
    
                    pages = imgname.Length / 5;
                    int rem = pageno % 8;
                    int x = 1;
                    if (pageno >= 1 && pageno <= 8)
                    {
                        x = 1;
                    }
                    else
                    {
                        if (rem == 0)
                        {
                            x = (pageno - 8) + 1;
                        }
                        else
                        {
                            x = pageno - (rem - 1);
                        }
                    }
    Last edited by Stewart Ross; Feb 9 '12, 07:20 PM. Reason: added code tags
  • Samuel Jones
    New Member
    • Jan 2011
    • 48

    #2
    A simple yet commonly overlooked solution to this is your pageno variable.

    This line:
    Code:
    if (imgname[pageno - 1, 0] != null)
    will throw the exception when pageno is 0 or less.

    This is because the exception is thrown even before the program checks to see if the reference exists or not.

    To rectify the issue, change this line to:
    Code:
    if (pageno > 0 && imgname[pageno - 1, 0] != null)
    this will be false when pageno is 0 or less and when the imgname reference is null.

    if pageno is not >0, it won't check the imgname reference and return false.


    P.S. Before you make posts in the future, please wrap your code in CODE tags, its a lot neater and easier to find problems.

    Comment

    Working...