title: App crashes when writing sensor data on a .txt file in a public folder
body: <p>I am very new to Java &amp; Android but I need to write a txt file which contains sensor data in Environment.DIRECTORY_DOCUMENTS. I scanned questions about it and tried examples. Version used here is Android 5.1 (API 22). My final trial is here:</p>
  <pre><code>package com.example.accgy;
  
  import androidx.appcompat.app.AppCompatActivity;
  
  import android.content.Context;
  import android.hardware.Sensor;
  import android.hardware.SensorEvent;
  import android.hardware.SensorEventListener;
  import android.hardware.SensorManager;
  import android.os.Bundle;
  import android.os.Environment;
  import android.view.View;
  import android.widget.TextView;
  import java.io.File;
  import java.io.FileWriter;
  import java.io.IOException;
  
  public class MainActivity extends AppCompatActivity implements SensorEventListener {
      private static final String TAG = &quot;MainActivity&quot;;
      private SensorManager sensorManager;
      Sensor accelerometer;
      Sensor gyroscope;
      FileWriter writer;
      TextView Xa,Ya,Za,Xg,Yg,Zg;
      float[] accel = new float[3];
      float[] gyro = new float[3];
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Xa = (TextView)findViewById(R.id.Xa);
          Ya = (TextView)findViewById(R.id.Ya);
          Za = (TextView)findViewById(R.id.Za);
          Xg = (TextView)findViewById(R.id.Xg);
          Yg = (TextView)findViewById(R.id.Yg);
          Zg = (TextView)findViewById(R.id.Zg);
          sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
          accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
          gyroscope = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
  
      }
  
      @Override
      public void onAccuracyChanged(Sensor sensor, int i) {
  
      }
  
  
      public void onStart(View view){
          super.onStart();
          sensorManager.registerListener(this,accelerometer,SensorManager.SENSOR_DELAY_NORMAL);
          sensorManager.registerListener(this,gyroscope,SensorManager.SENSOR_DELAY_NORMAL);
          String savedirect = getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS).toString();
  
  
  
  
          try {
              File folder = new File(savedirect,&quot;/example.txt&quot;);
              folder.mkdir();
              writer = new FileWriter(folder);
          } catch (IOException e) {
              e.printStackTrace();
          }
  
  
      }
  
      public void onStop(View view) {
          super.onStop();
  
          sensorManager.unregisterListener(this);
          try {
              writer.flush();
              writer.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      @Override
      public void onSensorChanged(SensorEvent sensorEvent) {
  
          if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
              accel = sensorEvent.values;
              Xa.setText(&quot;Xa: &quot; + accel[0]);
              Ya.setText(&quot;Ya: &quot; + accel[1]);
              Za.setText(&quot;Za: &quot; + accel[2]);
  
          }
          if (sensorEvent.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
              gyro = sensorEvent.values;
              Xg.setText(&quot;Xg: &quot; + gyro[0]);
              Yg.setText(&quot;Yg: &quot; + gyro[1]);
              Zg.setText(&quot;Zg: &quot; + gyro[2]);
          }
          try {
              writer.write(accel[0]+&quot;  &quot;+accel[1]+&quot;  &quot;+accel[2]+&quot;  &quot;+gyro[0]+&quot;  &quot;+gyro[1]+&quot;  &quot;+gyro[2]+&quot;\n&quot;);
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }
    
  </code></pre>
  <p>But when I run the code over a physical device and touch the start button, the app crashes. Is there a way to prevent this?</p>
  <p>Note: I also provide permission for writing external storages in manifest file.</p>
  <p>EDIT: I checked logcat and found:</p>
  <pre><code>Java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.FileWriter.write(java.lang.String)' on a null object reference
  </code></pre>
  <p>at:</p>
  <pre><code> try {
          writer.write(accel[0]+&quot;  &quot;+accel[1]+&quot;  &quot;+accel[2]+&quot;  &quot;+gyro[0]+&quot;  &quot;+gyro[1]+&quot;  &quot;+gyro[2]+&quot;\n&quot;);
      } catch (IOException e) {
          e.printStackTrace();
      }
  </code></pre>
  <p>When I change this line:</p>
  <pre><code>writer = new FileWriter(folder);
  </code></pre>
  <p>to this:</p>
  <pre><code>writer = new FileWriter(folder+&quot;example.txt&quot;,true);
  </code></pre>
  <p>App succesfully writes the data. But the name of file becomes <em>&quot;Documentsexample.txt&quot;</em>. But my desire is <em>&quot;example.txt&quot;</em>. I tried to remove &quot;folder+&quot; part from the line, tried to create folder in different ways. But same error returns after re-run. It seems no way other than above.</p>