简体   繁体   English

使用jsoup get()函数时出现IndexOutOfBoundsException

[英]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. 基本上,我的函数从https://meta.wikimedia.org/wiki/Table_of_Wikimedia_projects加载网页,并获取表格,以便在行中的某个单元格不为空时打印语言名称。 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()); 发生的是我得到一个IndexOutOfBoundsException错误,特别是在for循环的第二条语句中: 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)

notice that you have selected all <tr> 请注意,您已选择所有<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. 然后,它的第一行将是不具有<td>的标头,然后,在其第一次迭代中,当它尝试获取第三个元素<td>时,它出现了IndexOutOfBoundsException异常,因为它在那里不存在。

Just exclude the first <tr> that is header 只排除标题的第一个<tr>

// 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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM