title: Null Pointer Exception when writing to a File - Android Studio
body: <p>My app crashes every time I go to send the data gathered by the sensor. The error I am given is as follows:</p>
  
  <pre><code>06-20 14:50:00.784  22983-22983/com.example.adam.proj2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
  java.lang.NullPointerException
          at com.example.adam.proj2.SensorActivity.onClick(SensorActivity.java:124)
          at android.view.View.performClick(View.java:3549)
          at android.view.View$PerformClick.run(View.java:14393)
          at android.os.Handler.handleCallback(Handler.java:605)
          at android.os.Handler.dispatchMessage(Handler.java:92)
          at android.os.Looper.loop(Looper.java:154)
          at android.app.ActivityThread.main(ActivityThread.java:4944)
          at java.lang.reflect.Method.invokeNative(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:511)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
          at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Here is the code for gathering the sensor data:</p>
  
  <pre><code> public void onSensorChanged(SensorEvent event) {
      float x = event.values[0];
      ArrayList&lt;Float&gt; enrolAcc = new ArrayList&lt;&gt;();
      ArrayList&lt;Float&gt; authAcc = new ArrayList&lt;&gt;();
      TextView textEnrol = (TextView) findViewById(R.id.textView);
      if (choice == 1) {
          mPreviousAcc = mCurrentAcc;
          mCurrentAcc = (float) Math.sqrt((double) (x * x));
          float delta = mCurrentAcc - mPreviousAcc;
          mDiffAcc = mDiffAcc * 0.9f + delta;
          if (enrolAcc.size() &lt; 100) {
              enrolAcc.add(x);
  
          } else {
              enrolAcc.remove(0);
              enrolAcc.add(x);
          }
          walkData = enrolAcc.toString();
          textEnrol.setText(walkData);
      }
  </code></pre>
  
  <p>Here is the code for writing to the file (this happens onClick of a button):</p>
  
  <pre><code> public void onClick(View v) {
      switch (v.getId()) {
          case R.id.enrolBtn:
              choice = 1;
              Toast.makeText(this, "Enrolment Mode Selected", Toast.LENGTH_SHORT).show();
              break;
          case R.id.authBtn:
              choice = 2;
              Toast.makeText(this, "Authentication Service Starting", Toast.LENGTH_SHORT).show();
              break;
          case R.id.sendBtn:
              choice = 3;
              String baseDir = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
              String fileName = "Walk Data.csv";
              String filePath = baseDir + File.separator + fileName;
              File f = new File(filePath);
              FileOutputStream out = null;
              try {
                  out = new FileOutputStream(f);
              } catch (FileNotFoundException e) {
                  e.printStackTrace();
              }
              try {
                  out.write(walkData.getBytes());
              } catch (IOException e) {
                  e.printStackTrace();
              }
              try {
                  out.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
  
              android.net.Uri u1 = Uri.fromFile(f);
              Intent sendIntent = new Intent(Intent.ACTION_SEND);
              sendIntent.putExtra(Intent.EXTRA_STREAM, u1);
              sendIntent.setType("text/html");
              startActivity(sendIntent);
              break;
      }
  }
  </code></pre>
  
  <p>From what I can see the exception is generated by the out.write method?
  The array list holding the sensor values is stored in the walkData string so that the string can be then written in the csv file stored on the external device storage. I would like the data to be in CSV format.</p>
  
  <p>I am stumped and cannot figure out how to prevent this, any help would be much appreciated.</p>