This site requires JavaScript, please enable it in your browser!
Greenfoot back
Spock
Spock wrote ...

2014/9/7

java.lang.ArrayIndexOutOfBoundsException

Spock Spock

2014/9/7

#
Hi all. I'm trying to get a sound to play but whenever it plays it comes up with "Exception in thread java.lang.ArrayIndexOutOfBoundsException" error. I cna't determine the reason why. This error occurs in the test spelling method. Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//words for the spelling test
private String [] word = {"A", "Add", "Able", "Cast", "Brother", "Family", "Mayor", "Firm", "Many", "Cat"};
//variables for amount correct
private int correct = 10;
public int questionnum = 0;
public boolean nextQ = false;
public static int test =0;
private String question = "";
 
GreenfootSound Right = new GreenfootSound("Correct.mp3");
GreenfootSound Wrong = new GreenfootSound("Wrong.mp3");
/**
 * Constructor for objects of class QuizWorld.
 *
 */
public QuizWorld()
{   
    // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
    super(600, 400, 1);
     
    addObject(new Message("Press 1 to test Spelling\n Press 2 to test Math", 1), getWidth()/2, getHeight()/2);
}
public void act()
{
    if (nextQ == true)
    {
        if (test == 1)
        {
            testSpelling();
            QuizInput.Quiztype = 1;
        }
        if (test == 2)
        {
            testMath();
            QuizInput.Quiztype = 2;
        }
    }
}
public void testMath()
{
     
}
public void testSpelling()
{
    if (questionnum < 10)
    {
        question = word[questionnum] ;
        GreenfootSound questionsound = new GreenfootSound(question +".mp3");
        questionsound.play();
         
        questionnum = questionnum + 1;
        nextQ = false;
    }
}
K_wow K_wow

2014/9/7

#
While I can't seem to be able to find an error with this code, I will point out that the "Cat" sound will never play, as its array value is 10 and the code only runs when questionnum is 9 or lower.
Super_Hippo Super_Hippo

2014/9/7

#
You could insert the following line right before line 47 to see what the index was:
1
System.out.println(questionnum);
Maybe you change its value from somewhere else, too? @K_wow: The index starts with 0, so "Cat" is 9.
Spock Spock

2014/9/7

#
When I insert your code @Super_Hippo it just prints out 0.
Super_Hippo Super_Hippo

2014/9/7

#
What happens if you insert 'questionnum=1;' there? Which word will be displayed then?
Spock Spock

2014/9/7

#
Add is displayed there. I manged to solve the error by converting all the mp3 files to wav. Seems there's a bug with greenfoot and reading mp3 files.
K_wow K_wow

2014/9/7

#
Super_Hippo wrote...
You could insert the following line right before line 47 to see what the index was:
1
System.out.println(questionnum);
Maybe you change its value from somewhere else, too? @K_wow: The index starts with 0, so "Cat" is 9.
Yeah, I knew that, but.. I could have sworn that there was another one... :/
Spock wrote...
Add is displayed there. I manged to solve the error by converting all the mp3 files to wav. Seems there's a bug with greenfoot and reading mp3 files.
By the way, that's not a bug. Greenfoot simply doesn't support mp3 files.
Spock Spock

2014/9/7

#
K_wow wrote...
By the way, that's not a bug. Greenfoot simply doesn't support mp3 files.
Mp3 files do work. I have done it in the past.
Super_Hippo Super_Hippo

2014/9/7

#
API wrote...
Most files of the following formats are supported: AIFF, AU, WAV, MP3 and MIDI.
"Most files" ;)
danpost danpost

2014/9/7

#
Your mp3 files probably had some information tags in them that greenfoot did not know how to deal with. They were probably removed when you converted them to wav files. You may be able to convert to resultant wav files back to mp3 files (without the information tags) and then they also would work.
Spock Spock

2014/9/7

#
Fascinating. Thanks danpost and Super_Hippo for the information! :)
danpost danpost

2014/9/7

#
@Spock, to possibly save time in resolving problems, from now on, please show the entire error message using copy/paste operations. Showing the trace thread with the error message would have pointed us to exactly what was going on immediately (instead of searching through the code to find what might be wrong). In fact something as simple as the index '580' (or whatever) at the end of the error message might have helped in itself; but, still, the more information you give, the better.
You need to login to post a reply.