0

So I am learning threads in Java in and I revisited one of our old lessons in class where we created sounds. Whenever I run the program I get a ArrayIndexOutOfBoundsException: 2730 on my threads. This is the code I have in the class

public class ThreadA implements Runnable {
private Thread t;
private String threadName;

public ThreadA(String name) {
    threadName = name;
    System.out.println("Creating " + threadName);
}

public void run() {
    try {
        if(threadName.equalsIgnoreCase("Thread 1")){
           for(int i = 0; i < 2; i++) {
              Beeper.beep(200,800) //this is a call to the Beeper Class, its useless for this examples
            }
          }
        if(threadName.equalsIgnoreCase("Thread 2")){
            for(int i = 0; i < 2; i++) {
                Beeper.beep(233, 800); //line 37, where the error is
            }
        }
    catch (InterruptedException e){
        System.out.println("Thread " + threadName + " interrupted");
    }
      System.out.println("Thread " + threadName + " exiting");
}
 public void start(){
    System.out.println("Starting " + threadName);
    if(t == null){
        t = new Thread(this, threadName);
        t.start();
    }
}

The exact error is

Exception in thread "Thread 2" java.lang.ArrayIndexOutOfBoundsException: 2730
at Beeper.play(Beeper.java:88)
at Beeper.play(Beeper.java:104)
at Beeper.beep(Beeper.java:275)
at ThreadA.run(ThreadA.java:37)
at java.base/java.lang.Thread.run(Thread.java:844)

The line Beeper:88 - Beeper:89 is

buffer[bufferSize++] = (byte) s;
buffer[bufferSize++] = (byte) (s >> 8);   // little Endian

I figured it out. The Beeper class already created the array and the second thread tried to create a new array. Thanks for the help @Ken White

  • 1
    You are creating a `Thread` as a class variable _inside a class that already extends `Thread`_. This does not make sense. – Slaw Jun 03 '18 at 04:10
  • 1
    ArrayIndexOutOfBounds means you ran past the end of the array, and the value that your code tried to access was at index 2730. The error happened in Beeper.java on line 88. Use the debugger to figure out why you're trying to access that index when it doesn't exist. – Ken White Jun 03 '18 at 04:11
  • My bad I meant to implement Runnable. Beeper is a class that plays a sound at the frequency and the class at 89 shows this buffer[bufferSize++] = (byte) (s >> 8); . I have no clue what that is – DiamondMastiff Jun 03 '18 at 04:14

1 Answers1

0

What I did was just create a new SubClass of Beeper.

if (threadName.equalsIgnoreCase("Thread 2")) {
            Beeper beep2 = new Beeper1();
            beep2.beep(233, 800);
            beep2.beep(262, 800);
            beep2.beep(294, 800);
            beep2.sleep(1600);
            beep2.beep(233, 800);
            beep2.beep(262, 800);
            beep2.beep(294, 800);
            beep2.beep(294, 400);
            beep2.beep(294, 400);
            beep2.beep(294, 400);
            beep2.beep(294, 400);
            beep2.beep(262, 400);
            beep2.beep(262, 400);
            beep2.beep(262, 400);
            beep2.beep(262, 400);
            beep2.beep(233, 800);
            beep2.beep(262, 800);
            beep2.beep(294, 800);
            Thread.sleep(50);
        }

Thanks for the help guys