0

I'm getting Exception in thread "Thread-0" java.lang.NullPointerException.

enter image description here

this array is already printed before, I don't understand why int the second time it's throws this error. here is my code:

  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);
            }
        }   

and another question, why thread 1 gets the data that thread 2 should get?

Turing85
  • 18,217
  • 7
  • 33
  • 58
user4810410
  • 31
  • 1
  • 5
  • Only you can tell us why the variable on the involved line is null. And you're indicating the wrong line that *initiated* the exception. Please read the duplicate to see the general process for debugging a NPE, and then try to apply this process to your problem. – Hovercraft Full Of Eels Dec 30 '17 at 18:48
  • solved it. I initialized it twice . "proximity_Sensors=new Sensors[2][8];", so it changes the previous values – user4810410 Dec 30 '17 at 20:40

0 Answers0