title: NullPointerException with dos.write
body: <p>This code is written to record a file and return a sequence of the values from the audio file so that I can do some signal processing.
  I  keep getting a null pointer exception on this line towards the bottom:</p>
  
  <pre><code>dos.write(buffer, 0, bufferSize);
  </code></pre>
  
  <p>Can someone explain what is wrong and what I should change?</p>
  
  <pre><code>package com.example.wesle.noisemachine;
  
  import android.content.Intent;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.media.MediaPlayer;
  import android.media.MediaRecorder;
  import android.os.Environment;
  import android.widget.Toast;
  
  import java.io.DataOutputStream;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.IOException;
  
  public class ReceiveScreen extends AppCompatActivity {
  
      private Button buttonStart, buttonStop, buttonPlay, buttonDecode;
      private MediaRecorder mediaRecorder = new MediaRecorder();
      private String outputFile;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_receive_screen);
  
          buttonStart = (Button) findViewById(R.id.buttonStart);
          buttonStop = (Button) findViewById(R.id.buttonStop);
          buttonPlay = (Button) findViewById(R.id.buttonPlay);
          buttonDecode = (Button) findViewById(R.id.buttonDecode);
          buttonStop.setEnabled(false);
          buttonPlay.setEnabled(false);
          outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";
          mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
          mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
          mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
          mediaRecorder.setOutputFile(outputFile);
  
          buttonStart.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  try {
                      mediaRecorder.prepare();
                      mediaRecorder.start();
                  } catch (IllegalStateException ise) {
                      System.out.println("ISE Catch");
                  } catch (IOException ioe) {
                      System.out.println("IO Catch");
  
                  }
                  buttonStart.setEnabled(false);
                  buttonStop.setEnabled(true);
  
  
                  Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
  
  
              }
          });
  
          buttonStop.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
  
                  mediaRecorder.stop();
                  mediaRecorder.release();
  
                  buttonStop.setEnabled(false);
                  buttonPlay.setEnabled(true);
                  buttonStart.setEnabled(true);
  
                  Toast.makeText(getApplicationContext(), "Recording Completed", Toast.LENGTH_LONG).show();
              }
          });
  
          buttonPlay.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  MediaPlayer mediaPlayer = new MediaPlayer();
  
                  try {
                      mediaPlayer.setDataSource(outputFile);
                      mediaPlayer.prepare();
                      mediaPlayer.start();
                  } catch (Exception e) {
                      System.out.println("E Catch");
                  }
                  Toast.makeText(getApplicationContext(), "Recording Playing", Toast.LENGTH_LONG).show();
              }
          });
  
          buttonDecode.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  //System.out.println(readFile("recording.3gp"));
                  Toast.makeText(getApplicationContext(), "Decoding", Toast.LENGTH_LONG).show();
  
  
                  //initializes variables
                  File file = new File(outputFile);
                  FileInputStream fileInputStream = null;
                  try {
                      fileInputStream = new FileInputStream(file);
                  } catch (FileNotFoundException e) {
                      e.printStackTrace();
                  }
                  int bytesRead = 0;
                  int bufferSize = 0;
                  int bytesAvailable = 0;
                  byte[] buffer;
                  int maxBufferSize = 1*1024*1024;
                  DataOutputStream dos = null;
  
  
                  // create a buffer of maximum size
                  try {
                      bytesAvailable = fileInputStream.available();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
                  bufferSize = Math.min(bytesAvailable, maxBufferSize);
                  buffer = new byte[bufferSize];
  
                  // read file and write it into form...
                  try {
                      bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
                  try{
                      while (bytesRead &gt; 0){
                      dos.write(buffer, 0, bufferSize);  //&lt;&lt;&lt;&lt;&lt;&lt;&lt;HERE
                      bytesAvailable = fileInputStream.available();
                      bufferSize = Math.min(bytesAvailable, maxBufferSize);
                      try {
                          bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                      }
                  fileInputStream.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
  
              }
          });
  
          //Code for the back button
          Button backbuttonR = (Button) findViewById(R.id.backbuttonR);
          backbuttonR.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  startActivity(new Intent(ReceiveScreen.this, MainActivity.class));
              }
          });
      }
  }
  </code></pre>