线程中的异常"线程0" java.lang.Thread.run中的java.lang.NullPointerException(Thread.java:748)

时间:2017-12-30 18:44:52

标签: java multithreading concurrency nullpointerexception

我在线程"线程0"中得到了异常。显示java.lang.NullPointerException。

enter image description here

之前已经打印过这个数组,我不明白为什么第二次它会抛出这个错误。这是我的代码:

  static Sensors[][] proximity_Sensors;
    public static void main(String[] args)
    {
        StartGUI Quad_StartWindow=new StartGUI();

        Quad_StartWindow.StartSimulation.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent e) {

            Thread thread1=new Thread(new Runnable() {
                    public void run() {
                        if (ConnectToSimulator(1,Quad_StartWindow)!=-1)
                        {
                            ConnecSucc(1);
                            getSensors(1);
                            while (System.currentTimeMillis()-startTime < 1000000){

                                ReadSensor(1); // here the error

                            }
                        }
                    }
                });

            Thread thread2=new Thread(new Runnable() {
                    public void run() {
                        if (ConnectToSimulator(2,Quad_StartWindow)!=-1)
                        {
                            ConnecSucc(2);
                            getSensors(2);
                            while (System.currentTimeMillis()-startTime < 1000000){
                                ReadSensor(2);
                            }
                        }                      
                    }
                });

                thread1.start();
                thread2.start();                
            } 
        });
        Quad_StartWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Quad_StartWindow.setSize(500, 250);
        Quad_StartWindow.setTitle("Drone window");
        Quad_StartWindow.setVisible(true);
    }


public  synchronized static void getSensors(int j)
        {
            proximity_Sensors=new Sensors[2][8];
            for(int i=0;i<8;i++)
            {
                proximity_Sensors[j-1][i]=new Sensors();
                String nameObject="Proximity_sensor"+j+"_"+i;
                vrep.simxGetObjectHandle(clientID, nameObject, proximity_Sensors[j-1][i].intObjHandle, vrep.simx_opmode_oneshot_wait);
                System.out.println(proximity_Sensors[j-1][i].intObjHandle.getValue() + " " + j+" "+nameObject+" "+i);

            }
        }
        public  synchronized static void ReadSensor(int j){

            for(int i=0;i<8;i++)
            {
                ret=vrep.simxReadProximitySensor(clientID,  proximity_Sensors[j-1][i].intObjHandle.getValue(), proximity_Sensors[j-1][i].mLightSensorDetectionState, proximity_Sensors[j-1][i].detectedArray, proximity_Sensors[j-1][i].ObjectDetected, proximity_Sensors[j-1][i].detectedSurfArray, vrep.simx_opmode_oneshot_wait);
            }
        }   

和另一个问题,为什么线程1获取线程2应该得到的数据?

0 个答案:

没有答案