title: How can I read and write data about the accelerometer in android?
body: <p>I am trying to write a step counter for android. It currently includes four buttons in one activity:
  a button to start recording accelerometer data which is stored in an arraylist. The arraylist takes a type Trace, which is a class I created to hold the data of one sensor change. I also have a stop button, and buttons to read or write the data from text file.</p>
  
  <p>This program keeps giving me a NullPointerException on the arraylist and I can't figure out why. Any help would be greatly appreciated!</p>
  
  <p>EDIT: Sorry if the indentation is off or the code is unclear, this is a school assignment and I'm on a strict deadline so I have to rush to make the code usable before I can worry about readability or efficiency.</p>
  
  <p>EDIT 2: I no longer get any exceptions, however I still cannot read/write properly. I was able to write to file one successfully, and then somehow it stops functioning.</p>
  
  <pre><code>package com.myApp.playpool;
  
      //imports
  
  public class MainActivityBAK extends Activity implements SensorEventListener{
  
      //global fields (traces is instantiated here as new arraylist)
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          acceleration=(TextView)findViewById(R.id.acceleration);
          startButton = (Button) findViewById(R.id.startButton);
          stopButton = (Button) findViewById(R.id.stopButton);
          readButton = (Button) findViewById(R.id.readButton);
          writeButton = (Button) findViewById(R.id.writeButton);
  
          sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
          accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
          sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
  
          dataFilePath = getString(R.string.data_file_path);
          acceleration.setText("Current file: " + dataFilePath);
  
          lastCheck = System.currentTimeMillis();
  
          defineButtons();
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
  
          getMenuInflater().inflate(R.menu.activity_main, menu);
          return true;
      }
      @Override
      public void onSensorChanged(SensorEvent event1) {
  
          if (running &amp;&amp; ((System.currentTimeMillis() - lastCheck) &gt; 1000)) { 
              acceleration.setText("X: "+event1.values[0]+"\nY: "+event1.values[1]+"\nZ: "+event1.values[2]);
              traces.add(new Trace(System.currentTimeMillis(), event1.values[0], event1.values[1], event1.values[2]));
              lastCheck = System.currentTimeMillis();
          }
  
      }
  
      @Override
      public void onAccuracyChanged(Sensor sensor, int accuracy) {
  
      }
  
      public void defineButtons() { //defines onClick methods for the buttons
  
          startButton.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
                  traces = new ArrayList&lt;Trace&gt;();
                  running = true;
              }
          });
  
          stopButton.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
                  traces = new ArrayList&lt;Trace&gt;();
                  running = false;
              }
          });
  
          readButton.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
                  try {
                      scan = new Scanner(new File(dataFilePath));
                  } catch (FileNotFoundException e) {
                      e.printStackTrace();
                  }
                  while (scan.hasNext()) {
                      String str = scan.nextLine();
                      String [] strings = str.split(";");
                      double time = Double.parseDouble(strings[0]);
                      float x = Float.parseFloat(strings[0]), y = Float.parseFloat(strings[1]), z = Float.parseFloat(strings [2]);
                      traces.add(new Trace(time, x, y, z));
                  }
  
                  Toast.makeText(getBaseContext(),
                          ("Done reading to SD file: '" + dataFilePath + "'"),
                          Toast.LENGTH_SHORT).show();
                  scan.close();
              }
          });
  
          writeButton.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
                  try {
                      File file = new File(dataFilePath);
                      print = new PrintWriter(file);
                      file.createNewFile();
                  } catch (FileNotFoundException e) {
                      e.printStackTrace();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
                  for (int i = 0; i &lt; traces.size(); i++) {
                      double time = traces.get(i).time;
                      float x = traces.get(i).x, y = traces.get(i).y, z = traces.get(i).z;
                      print.println(time + ";" + x + ";" + y + ";" + z + ";");
                  }
                  print.close();
                  Toast.makeText(getBaseContext(),
                          ("Done writing to SD file: '" + dataFilePath + "'"),
                          Toast.LENGTH_SHORT).show();
              }
          });
  
      }
  
  }
  </code></pre>
  
  <p>It seems to be crashing in the for loop in the write method:   </p>
  
  <pre><code>for (int i = 0; i &lt; traces.size(); i++) { 
  </code></pre>