Search code examples
javajsoupindexoutofboundsexception

IndexOutOfBoundsException when using jsoup get() function


Basically my function loads a webpage from https://meta.wikimedia.org/wiki/Table_of_Wikimedia_projects and takes the table so that it prints the name of the language if a certain cell in its row is not empty. Here is the code:

public static void getLanguagesFromProject(String project) {
    String html = "https://meta.wikimedia.org/wiki/Table_of_Wikimedia_projects";
    try {
        Document doc = Jsoup.connect(html).get();
        Elements tableElements = doc.select("table.wikitable.sortable");
        Elements rows = tableElements.select("tr");
        int column = 0;
        switch (project) {
            case "Wikipedia":
                column = 3;
                break;
            case "Wiktionary":
                column = 4;
                break;
            case "Wikibooks":
                column = 5;
                break;
            case "Wikinews":
                column = 6;
                break;
            case "Wikiquote":
                column = 7;
                break;
            case "Wikisource":
                column = 8;
                break;
            case "Wikiversity":
                column = 9;
                break;
            case "Wikivoyage":
                column = 10;
                break;
            default:
                break;
        }
        for (Element row : rows) {
            Elements cols = row.select("td");
            System.out.println(cols.get(column).text());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

What happens is that I get an IndexOutOfBoundsException error, specifically in the second statement in the for loop: System.out.println(cols.get(column).text()); Any idea what needs to be done? Edit: the error in more detail:

java.lang.IndexOutOfBoundsException: Index 3 out-of-bounds for length 0 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248) at java.base/java.util.Objects.checkIndex(Objects.java:372) at java.base/java.util.ArrayList.get(ArrayList.java:440) at com.company.Main.getLanguagesFromProject(Main.java:76) at com.company.Main.main(Main.java:11)


Solution

  • notice that you have selected all <tr>

    Elements tableElements = doc.select("table.wikitable.sortable");
    Elements rows = tableElements.select("tr");
    

    including those in the header. Then its first row will be the header that does not have <td>, then, in its first iteration, it got an IndexOutOfBoundsException exception when it tries to get the 3rd element <td> because it does not exist there.

    Just exclude the first <tr> that is header

    // start from 1, exclude 0 which is a header without td's
    for (int i = 1; i < rows.size(); i++) {
        Elements cols = rows.get(i).select("td");
        System.out.println(cols.get(column).text());
    }