I am suppose to create a program that will take two parameters: the number of threads
n and and a matrix side length m. It will generate two random-value
square matrices A and B of size mm each, and feed equal-size
partitions to the threads, each thread computing a portion of the
product matrix C = A B. Also, we are assuming that n and m are powers of four, and
also n<m.

Here is my code:

package hwos2;

import java.io.*;
import java.util.*;


public class Main {


private static int i = 1,j = 1;
private static int array1[][] = new int[j][j];
private static int array2[][] = new int[j][j];
private static int array3[][] = new int[j][j];

private static class MultiplyMatrix implements Runnable {
int min, max, tnum = 0;

public MultiplyMatrix(int lower, int higher, int id) {
min = lower;
max = higher;
tnum = id;
}

public void run() {
for (int x = min; x < max; x++) {
for (int y = 0; y < j; y++) {
array3[x][y] = 0;
for (int count = 0; count <j; count++) {
array3[x][y] += array1[x][count] * array2[count][y];
}
}
}
}
}

public static void main(String args[]) throws InterruptedException
{

try
{
long start = System.currentTimeMillis();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Random randomGenerator = new Random();

System.out.print("Enter the order of Matrix m : ");

j=Integer.parseInt(br.readLine());


System.out.print("Enter the number of threads : ");

i=Integer.parseInt(br.readLine());


int array1[][] = new int[j][j];
int array2[][] = new int[j][j];
System.out.println("array1 is:");
for(int m=0;m<j;m++)
{
for(int n=0;n<j;n++)
{
array1[m][n]= randomGenerator.nextInt(10);
System.out.print(array1[m][n]+"\t");
}
System.out.print("\n");
}
System.out.println("array2 is:");
for(int m=0;m<j;m++)
{
for(int n=0;n<j;n++)
{
array2[m][n]= randomGenerator.nextInt(10);
System.out.print(array2[m][n]+"\t");
}
System.out.print("\n");
}
Thread[] threads = new Thread[j/i];

for (int c = 0; c < i-1; c++) {

threads[c] = new Thread( new MultiplyMatrix(j/i * c, j/i * (c + 1) , c) );
threads[c].start();
}
for (int c = 0; c < i; c++) {
threads[c].join();
}
System.out.println("The result is:");
for (int x = 0; x < 1000; x++) {
for (int y = 0; y < 1000; y++) {

System.out.println(array3[x][y]);
}
}
long end= System.currentTimeMillis() ;
long runTime= end - start;
System.out.println("Total time: " +runTime + " ms");

}


catch (Exception e) {
e.printStackTrace();
}
}

}

// code ends here



for some reason I receive the following error:
Exception in thread "Thread-1" java.lang.ArrayIndexOutOfBoundsException: 4
at hwos2.Main$MultiplyMatrix.run(Main.java:27)
at java.lang.Thread.run(Thread.java:619)
Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: 1
at hwos2.Main$MultiplyMatrix.run(Main.java:29)
at java.lang.Thread.run(Thread.java:619)
Exception in thread "Thread-2" java.lang.ArrayIndexOutOfBoundsException: 8
at hwos2.Main$MultiplyMatrix.run(Main.java:27)
at java.lang.Thread.run(Thread.java:619)
java.lang.NullPointerException
at hwos2.Main.main(Main.java:85)
BUILD SUCCESSFUL (total time: 9 seconds)

I do not understand why, and I can not see why it would be an outofbound exception, obviously it's crashing here threads[c] = new Thread( new MultiplyMatrix(j/i * c, j/i * (c + 1) , c) ); but I still can't make much sens of it. Could anybody help me get this to work asap, Thanks?