Java 在循环中创建多个线程对象

Java 在循环中创建多个线程对象,java,arrays,multithreading,nullpointerexception,thread-safety,Java,Arrays,Multithreading,Nullpointerexception,Thread Safety,我怎样才能解决这个问题。如何创建多个线程对象 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 NullPoin

我怎样才能解决这个问题。如何创建多个线程对象

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();
    }
}
public void filter控件(int threadCount){
this.rowCount=img.getHeight()/threadCount;
线程[]t=null;

对于(int i=0;i您的
NullPointerException
来自您将数组初始化为
null

将其初始化为:

Thread[] t = new Thread[threadCount];
这将初始化其所有元素为
null
(对象
的默认值),但数组本身为非
null
实例,总共有
threadCount
元素插槽

注意

您不能通过调用
run
来启动
线程
,该线程将在调用线程内执行
run
方法


使用
t[i].start();

在尝试使用“数组”之前创建一个数组

试着替换

Thread[] t = null;


请解释一下否决票?
Thread[] t = new Thread[threadCount];