【发布时间】:2014-05-02 14:54:05
【问题描述】:
我有以下代码,我得到了java.lang.ArrayIndexOutOfBoundsException
//divides ACode by 10 as many times as specified by the DecimalDigitPosition
public static int getDecimalDigit(int ACode, int decimalDigitPosition) {
if (decimalDigitPosition == 0)
return ACode;
else {
ACode = ACode / (10 * decimalDigitPosition);
int remainder = ACode % 10;
return remainder;
}
}
我怀疑这条线...
digit[i] = getDecimalDigit(samples[i].getValue(), 0);
在下面的代码中,这可能是导致此异常的原因。
//start a method to decode the message.
public static void decodeMessage(String filename) {
Sound s = new Sound(filename);
SoundSample[] samples = s.getSamples();
int[] digit = new int[3];
String message = "";
int sampleIndex = 0;
boolean nullReached = false;
while (!nullReached) {
int asciiValue = 0;
for (int i = sampleIndex; i < sampleIndex + 3; i++) {
if (i < samples.length) {
// this line could be the cause of error..
digit[i] = getDecimalDigit(samples[i].getValue(), 0);
asciiValue += digit[i - sampleIndex] * (((i - sampleIndex) == 0)
? 1
: ((i - sampleIndex) * 10));
} else {
for (int j = 0; j < 3; j++) {
digit[j] = 0;
}
break;
}
}
if (digit[0] == digit[1]
&& digit[1] == digit[2]
&& digit[2] == 0)
nullReached = true;
message += (char) asciiValue;
}
}
堆栈跟踪:
java.lang.ArrayIndexOutOfBoundsException: 3 在 project.decodeMessage(project.java:107) 在 project.main(project.java:33) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在 java.lang.reflect.Method.invoke(Method.java:597) 在 edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272) >
【问题讨论】:
-
哪些行会抛出这些错误?你可以发布堆栈跟踪吗?
-
如果您认为问题出在代码的某个部分,通常最好只包含该部分:)
-
这里是错误代码:java.lang.ArrayIndexOutOfBoundsException: 3 at project.decodeMessage(project.java:107) at project.main(project.java:33) at sun.reflect.NativeMethodAccessorImpl。 invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:第597章)
-
和泰勒,你说得对,哈哈,我没想到。现在编辑代码
标签: java indexoutofboundsexception