Index or size is negative or greater than allowed amount (non-negative indices)

16,810

Strictly working off of the currently posted code, here are the issues I see:

  1. The posted code looks incomplete. Where is rows being initialized? This could cause the stated error.

  2. Given while(j--);   The var j = rows.length - 1; line is incorrect. That is, unless you know that the last row will never need deleting. But if that is the case, then comment the code to make it clear.

    For example, if there were 4 rows, the current code initializes j to 3, but because of the location of the -- operator, the inside of the loop sees: 2, 1, 0.   For the code as shown, use var j = rows.length; or add a comment to show that the logic is deliberate.

  3. The 2 if() statements do not depend on j at all! (At least as the code is posted here.) If this is true, then move the conditionals outside of the j loop.

  4. Consider posting the full, unedited, code. Or linking to it on a site like Pastebin.



Update for full script, now that it's been linked to:

Scanning the complete code, it looks like tables[i].deleteRow(j); can be called multiple times for the same row.

The easy solution, that should be done anyway, is to add a continue statement after each row delete.

For extra credit, reanalyze and simplify the flag and if logic too. :)



Update for target page, now that it's been linked to:

Examining the target page, the tables being looped by this script contain nested tables.

That throws off the row count in this line:
var rows = tables[i].getElementsByTagName("tr");

Sometimes making it seem like table[i] has more rows than it really directly owns.

Solution, use the built in rows array; so the line becomes: var rows = tables[i].rows;

~~~~
While examining the script relative to the target page, a few other issues seemed apparent:

  1. It's not best to loop through all tables. Target just the ones you need. So this:
    tables = document.getElementsByTagName("table");

    Should be changed to:

    var tables = document.querySelectorAll ("div.KonaBody > table.roundy");
    

    ...which will select just the 4 payload tables, and not their subtables or the other tables scattered about.

  2. By fine-tuning the initial table selection, the following, problamatic, test is not needed:

    if(tables[i].getAttribute("style").indexOf("border: 3px solid") != -1)
    
  3. Missing var in front of the majorSections initialization.
Share:
16,810
KongMD
Author by

KongMD

I’m 23 years old and I have an Associate's Degree in Web Programming at STCC. I also have experience with Java, C#, Perl, and HTML. I'm currently working on my Bachelor's Degree in Computer Science at Springfield College. Feel free to contact me! I’m a friendly guy :)

Updated on October 11, 2022

Comments

  • KongMD
    KongMD over 1 year

    While using Firefox, I keep getting the error described in the subject line for this block of code:

    for(var i = 0; i < tables.length; i++)
    {
        var j = rows.length - 1;
        while(j--)
        {                   
            if(hideDP && tables[i].innerHTML.indexOf(">D<") != -1)
            {   
                if(!platTag && !soulSilverTag && pearlTag)
                {
                    tables[i].deleteRow(j);//ERROR IS ON THIS LINE
                }
            }
    
        }//end while loop (rows)
    }//end for loop (tables)
    

    I suspect that this error is because I'm somewhat new to making reverse loops, but I specifically made a reverse loop in this instance because it made deleting rows from a table easier. Note also that j is something like 24 and i is 0, so they're non-negative. Could someone shed some light on this for me?

    EDIT : The full code can be found here.