Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么线程创建的数组返回空指针异常?_Java_Arrays_Multithreading_Variables_Nullpointerexception - Fatal编程技术网

Java 为什么线程创建的数组返回空指针异常?

Java 为什么线程创建的数组返回空指针异常?,java,arrays,multithreading,variables,nullpointerexception,Java,Arrays,Multithreading,Variables,Nullpointerexception,这是我的代码 protected static Objects[] theObject; private static Runnable objectDeployment = () -> { MainThread.theObject = new Objects[noOfObjects]; for(int i=0; i<noOfObjects; i++) { theObject[i] = new Objects(); the

这是我的代码

protected static Objects[] theObject;

private static Runnable objectDeployment = () ->    {
    MainThread.theObject = new Objects[noOfObjects];
    for(int i=0; i<noOfObjects; i++)    {
        theObject[i] = new Objects();
        theObject[i].setEnableMovement(true);
        theObject[i].start();
    }
};

private static RAMDS[] track;

private static Runnable scanner = () -> {
    int no = noOfObjects;
    track = new RAMDS[no];
    for(int i=0; i<no; i++) {
        int[] position = MainThread.theObject[i].getPosition();
        int magnitude = MainThread.theObject[i].getMagnitude();

        //check to see if there is an object within range
        if(position[0] > 0 && position[0] < Wireframe.xlimit )
            if(position[1] > 0 && position[1] < Wireframe.ylimit )
                if(magnitude > 0)   {
                    track[i] = new RAMDS(); 
                    track[i].objectId(i);   
                    track[i].start();           
                }
    }
};

public static void main(String[] args) {
    // TODO Auto-generated method stub
    noOfObjects = getNoOfObjects();
    new Thread(objectDeployment).start();
    new Thread(scanner).start();
}

}

您需要等待objectDeployment线程完成。你有比赛条件。扫描器线程尝试使用对象[i]时未分配该对象

编辑: 将objectDeployment拆分为初始化阶段,并同步启动对象的objectStart。顺便说一句,我认为对象的名称非常混乱

protected static Objects[] theObject;

private static Runnable objectDeployment = () ->    {
    MainThread.theObject = new Objects[noOfObjects];
    for(int i=0; i<noOfObjects; i++)    {
        theObject[i] = new Objects();
        theObject[i].setEnableMovement(true);
    }
};

private static Runnable objectStart = () ->    {

    for(int i=0; i<noOfObjects; i++)    {
        theObject[i].start();
    }
};

private static RAMDS[] track;

private static Runnable scanner = () -> {
    int no = noOfObjects;
    track = new RAMDS[no];
    for(int i=0; i<no; i++) {
        int[] position = MainThread.theObject[i].getPosition();
        int magnitude = MainThread.theObject[i].getMagnitude();

        //check to see if there is an object within range
        if(position[0] > 0 && position[0] < Wireframe.xlimit )
            if(position[1] > 0 && position[1] < Wireframe.ylimit )
                if(magnitude > 0)   {
                    track[i] = new RAMDS(); 
                    track[i].objectId(i);   
                    track[i].start();           
                }
    }
};

public static void main(String[] args) {
    // TODO Auto-generated method stub
    noOfObjects = getNoOfObjects();
    objectDeployment.run()
    new Thread(objectStart).start();
    new Thread(scanner).start();
}

}
受保护的静态对象[]对象;
私有静态可运行objectDeployment=()->{
MainThread.theObject=新对象[NoofObject];
对于(int i=0;i{
对于(int i=0;i{
int no=无对象;
轨道=新RAMDS[否];
对于(int i=0;i 0&&position[0]0和位置[1]0){
磁道[i]=新的RAMDS();
轨道[i].objectId(i);
轨迹[i]。开始();
}
}
};
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
noOfObjects=getNoOfObjects();
objectDeployment.run()
新线程(objectStart.start();
新线程(扫描器).start();
}
}

为什么你假设线程1已经运行了?我忘了添加这个类mainthread如果你需要添加信息,你的问题是。为什么你假设线程之间共享状态而不进行任何同步是一件安全的事情?事实并非如此。每一个关于线程的体面教程都谈到了这一点。如果你想nt要认真对待线程。如果第二个线程必须等到第一个线程创建数组时才能执行,那么通过线程执行这项任务会有点困难。谢谢您的回复。问题是第一个线程创建对象,而其他几个线程很少更改此对象的状态。现在我正在尝试当这些更改发生时,用扫描仪跟踪它们“如果可以的话,就像雷达一样”。run()方法在线程仍处于运行状态时不会完成。我该怎么办?抱歉,我似乎不知道我在做什么?就是这样。谢谢。现在我看到了,它很混乱。我要尽快更改它。这是我在java中的第一个代码
protected static Objects[] theObject;

private static Runnable objectDeployment = () ->    {
    MainThread.theObject = new Objects[noOfObjects];
    for(int i=0; i<noOfObjects; i++)    {
        theObject[i] = new Objects();
        theObject[i].setEnableMovement(true);
    }
};

private static Runnable objectStart = () ->    {

    for(int i=0; i<noOfObjects; i++)    {
        theObject[i].start();
    }
};

private static RAMDS[] track;

private static Runnable scanner = () -> {
    int no = noOfObjects;
    track = new RAMDS[no];
    for(int i=0; i<no; i++) {
        int[] position = MainThread.theObject[i].getPosition();
        int magnitude = MainThread.theObject[i].getMagnitude();

        //check to see if there is an object within range
        if(position[0] > 0 && position[0] < Wireframe.xlimit )
            if(position[1] > 0 && position[1] < Wireframe.ylimit )
                if(magnitude > 0)   {
                    track[i] = new RAMDS(); 
                    track[i].objectId(i);   
                    track[i].start();           
                }
    }
};

public static void main(String[] args) {
    // TODO Auto-generated method stub
    noOfObjects = getNoOfObjects();
    objectDeployment.run()
    new Thread(objectStart).start();
    new Thread(scanner).start();
}

}