Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ArrayIndexOutOfBoundsException请帮助查找错误_Java_Android_Android Studio - Fatal编程技术网

Java ArrayIndexOutOfBoundsException请帮助查找错误

Java ArrayIndexOutOfBoundsException请帮助查找错误,java,android,android-studio,Java,Android,Android Studio,我有以下代码: String[] currentitem = new String[5]; currentitem = responsestring.split("%"); String[] date = new String[3]; date = currentitem[2].split("."); String[] time = new String[3]; time = currentitem[4].split(":"); objects.add(new Print(id, current

我有以下代码:

String[] currentitem = new String[5];
currentitem = responsestring.split("%");
String[] date = new String[3];
date = currentitem[2].split(".");
String[] time = new String[3];
time = currentitem[4].split(":");
objects.add(new Print(id, currentitem[0],currentitem[1], currentitem[5],             Integer.parseInt(currentitem[3]), Integer.parseInt(date[2]), Integer.parseInt(date[1]), Integer.parseInt(date[0]), Integer.parseInt(time[0]), Integer.parseInt(time[1]), Integer.parseInt(time[2])));`
我得到以下错误:

10-24 14:46:02.303    7841-7860/de.socialbit.printlog2 E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-646
    java.lang.ArrayIndexOutOfBoundsException: length=0; index=2
            at de.socialbit.printlog2.NavigationDrawerFragment$requestdata.run(NavigationDrawerFragment.java:367)
            at java.lang.Thread.run(Thread.java:856)

我似乎找不到错误在哪里

错误发生在这里:

date = currentitem[2].split(".");
如果您查看错误消息,您将看到它提到您正在使用的数组的长度是0,但您正在尝试获取元素2。这就是发生这种情况的地方

您的
currentitem
数组已从此行到达

currentitem = responsestring.split("%");
如果这是一个零长度数组,那么这只能是因为
responsestring
为空(即等于
“”

你感到困惑的部分原因是,你误解了这句话的意思

String[] currentitem = new String[5];
currentitem = responsestring.split("%");
这里的第二行没有填充您在第一行中创建的数组,它只是将其丢弃。
.split()
方法创建数组,而不是对预先提供的数组进行操作。因此,您为
currentitem
指定了长度这一事实是不相关的:您创建的
新字符串[5]
不再位于
.split()
行之后,该行将返回它认为合适的任何数组。你应该把这两行写成一行:

String[] currentitem = responsestring.split("%");
date
time
也是如此:您创建的数组将立即被丢弃,并替换为
.split()
调用返回的任何数组,该数组可能具有任何长度(包括零)


但是主要的问题似乎是,当你期望responsestring中包含某些内容时,
responsestring
是空的。

异常意味着你请求的位置超出了它的界限。。现在让我阅读对象末尾的问题el ol elcurrentItem[4]而不是currentItem[5]。add()
我似乎找不到错误所在
您正在尝试引用一个不存在的数组项
currentitem
声明的长度为5,您试图访问不存在的第六个数组项(
currentitem[5]
)。@zgc7009我已更改了它,但无论如何都会出错。我理解这一点,但date[]声明为字符串[3],因此应该是ok@JonasOtto我编辑了答案来解释。您的
字符串[3]
正在被丢弃。我已经用
currentitem
进行了解释,但同样的原则也适用。谢谢,这解释了一切:D@JonasOtto不客气:)