Вопрос

I have the following piece of code for which i am getting 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;
    }
}

I am suspecting this line...

digit[i] = getDecimalDigit(samples[i].getValue(), 0);

in the code below which could be the reason for this exception.

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

Stack trace:

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.jav‌​a:25) at java.lang.reflect.Method.invoke(Method.java:597) at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:27‌​2) >

Это было полезно?

Решение

The problem is likely here in your decodeMessage method. I've snipped out some code to isolate the problem:

int[] digit = new int[3];
while (!nullReached)
{
    for (int i = sampleIndex; i < sampleIndex + 3; i++)
    {
        if (i < samples.length)
        {
            digit[i] = getDecimalDigit(samples[i].getValue(), 0);   // This should be the line that throws the exception                
        }               
    }           
    sampleIndex += 3;           
}

The second time through your while loop, sampleIndex will be 3, so i will start at 3. Then you try to dereference digit[i], which would be digit[3]. Since digit[] is an int[3], then digit[3] is out of the bounds of the array.

Maybe you want to do this instead:

digit[i-sampleIndex] = getDecimalDigit(samples[i].getValue(), 0);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top