0

I have a class receiving data from the accelerometer at my phone. Sensor data are put into queuex, queuey and queuez.

public class AcceInterface implements SensorEventListener {
    private SensorManager sensorManager;
    private Sensor accelerometer;
    public Queue<Float> queuex;
    public Queue<Float> queuey;
    public Queue<Float> queuez;


    @Override
    public void onSensorChanged(SensorEvent event) {
        queuex.add(Float.valueOf(event.values[0]));
        queuey.add(Float.valueOf(event.values[1]));
        queuez.add(Float.valueOf(event.values[2]));
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}

In my mainactivity, I get the accelerometer data from the queues and plot in a runnable thread.

acce = new AcceInterface();

class Plot implements Runnable
{

        Thread mythread ;
        Plot()
        {
            mythread = new Thread(this, "my runnable thread");
            System.out.println("my thread created" + mythread);
            mythread.start();
        }

        public void run()
        {
            while (systemON) {

                if (!acce.queuex.isEmpty()) {
                    mSeriesX.add(x_axis, acce.queuex.poll().floatValue());
                    mSeriesY.add(x_axis, acce.queuey.poll().floatValue());
                    mSeriesZ.add(x_axis++, acce.queuez.poll().floatValue());
                    if (x_axis % 50 == 0) {
                        mRenderer_x.setXAxisMin(x_axis);
                        mRenderer_x.setXAxisMax(x_axis + 50);
                        mRenderer_y.setXAxisMin(x_axis);
                        mRenderer_y.setXAxisMax(x_axis + 50);
                        mRenderer_z.setXAxisMin(x_axis);
                        mRenderer_z.setXAxisMax(x_axis + 50);
                    }
                    //if (mChartView_x != null) {
                        mChartView_x.repaint();
                    //}
                   // if (mChartView_y != null) {
                        mChartView_y.repaint();
                    //}
                    //if (mChartView_z != null) {
                        mChartView_z.repaint();
                    //}

                }
            }

        }
}

I let the program run and sometime I got error as

04-01 22:11:16.927 8936-14743/com.forcetest E/AndroidRuntime: FATAL EXCEPTION: my runnable thread
                                                                     java.lang.NullPointerException
                                                                         at com.forcetest.RawData$Plot.run(RawData.java:339)
                                                                         at java.lang.Thread.run(Thread.java:856)

What is wrong with my thread? I used ConcurrentLinkedQueue and it is thread safe. What is wrong?

Thanks

batuman
  • 7,066
  • 26
  • 107
  • 229
  • I am going to close this as duplicate to the Solving a NPE question, unless you can explain how this is not another NPE question. – njzk2 Apr 01 '16 at 14:26
  • Clearly a duplicate. Queues are not initialized – 0xDEADC0DE Apr 01 '16 at 14:44
  • No queues are initialized. The problem is I checked only queuex( !acce.queuex.isEmpty() ). I need to check all three. – batuman Apr 01 '16 at 14:52

0 Answers0