获取java.lang.indexoutofboundsexception,但不知道为什么? List s=getConfig().getStringList(“clanowner”); 如果(!(s.isEmpty()){ 用于(字符串str:s){ String[]words=str.split(“:”); menu.clanowner.put(words[0],words[1]); } }

获取java.lang.indexoutofboundsexception,但不知道为什么? List s=getConfig().getStringList(“clanowner”); 如果(!(s.isEmpty()){ 用于(字符串str:s){ String[]words=str.split(“:”); menu.clanowner.put(words[0],words[1]); } },java,indexoutofboundsexception,Java,Indexoutofboundsexception,所以,我得到了一个java.lang.indexoutofboundsexception,但我不知道为什么会发生这种情况。有人能帮帮我吗?我的直觉是 List<String> s = getConfig().getStringList("clanowner"); if (!(s.isEmpty())) { for (String str : s) { String[] words = str.split(":"); menu.clanowne

所以,我得到了一个java.lang.indexoutofboundsexception,但我不知道为什么会发生这种情况。有人能帮帮我吗?

我的直觉是

List<String> s = getConfig().getStringList("clanowner");
if (!(s.isEmpty())) {

    for (String str : s) {
        String[] words = str.split(":");
        menu.clanowner.put(words[0], words[1]);
    }
}
可能没有返回长度为2的数组。将for语句的最后一行更改为

String[] words = str.split(":");
应消除IndexOutOfBoundsException

作为一种调试实践,它也有助于准确地了解其中的内容,因此请确保

if(words != null && words.length > 1) {
    menu.clanowner.put(words[0], words[1]);
}
List s=getConfig().getStringList(“clanowner”);

True是通过调试或手动插入应该可以工作的值,或者两者都可以,为您提供应该生成长度为2或2以上的数组的输出。

如果
words[]
数组仅包含一个元素,
words[1]
将不存在(超出范围)。确切的堆栈跟踪说明了什么?split函数可能返回一个包含一个元素而不是两个元素的数组。使用调试器,您将看到确切的原因。谢谢@Alex,我添加了一个try-and-catch语句。
List<String> s = getConfig().getStringList("clanowner");