-1

How can i get rid of this problem. How to create multiple thread objects?

public void filterControl (int threadCount) {
    this.rowCount = img.getHeight() / threadCount;
    Thread[] t = null;

    for (int i=0; i<threadCount; i++) {
        t[i] = new Thread(new Runnable(){ //there is a NullPointerException

            @Override
            public void run() {
                filtering(startRow, rowCount);
            }
        });

        t[i].run();
    }
}
mjdcsy
  • 17
  • 6

2 Answers2

2

Your NullPointerException comes from the fact that you initialized your array as null.

Initialize it instead as:

Thread[] t = new Thread[threadCount];

This will initialize all its elements as null (the default value for Objects), but the array itself as a non-null instance, with a total of threadCount element slots.

Note

You do not start a Thread by invoking run, which will execute the run method within the invoking thread.

Use t[i].start(); instead.

Mena
  • 47,782
  • 11
  • 87
  • 106
0

Create an array before trying to use the "array".

Try replacing

Thread[] t = null;

to

Thread[] t = new Thread[threadCount];
MikeCAT
  • 73,922
  • 11
  • 45
  • 70