Q: Having error during read/write to file in my App MainActivity: 
package com.example.jaypatel.file;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.Buffer;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void writeData(View view)
    {
        BufferedWriter bufferWriter = null;
        try {
            FileOutputStream fileOutputStream = openFileOutput("testFile", Context.MODE_PRIVATE);
            bufferWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
            bufferWriter.write(((EditText)this.findViewById(R.id.editText1)).getText().toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally
        {
            try {
                bufferWriter.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

    public void readData(View view)
    {
        BufferedReader bufferedReader = null;
        StringBuilder result = new StringBuilder();
        try {
            FileInputStream fileInputStream = openFileInput("testFile");
            BufferedReader bufferReader = new BufferedReader(new InputStreamReader(fileInputStream));
            String line;
            while ((line=bufferReader.readLine())!=null){
                result.append(line + "\r\n");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        ((EditText)this.findViewById(R.id.editText2)).setText(result);
    }
}

ERROR LOG:
FATAL EXCEPTION: main
Process: com.example.jaypatel.file, PID: 3382
java.lang.IllegalStateException: Could not execute method for android:onClick
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
    at android.view.View.performClick(View.java:4438)
    at android.view.View$PerformClick.run(View.java:18422)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
    at android.view.View.performClick(View.java:4438) 
    at android.view.View$PerformClick.run(View.java:18422) 
    at android.os.Handler.handleCallback(Handler.java:733) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:136) 
    at android.app.ActivityThread.main(ActivityThread.java:5017) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
    at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException
    at com.example.jaypatel.file.MainActivity.readData(MainActivity.java:65)
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
    at android.view.View.performClick(View.java:4438) 
    at android.view.View$PerformClick.run(View.java:18422) 
    at android.os.Handler.handleCallback(Handler.java:733) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:136) 
    at android.app.ActivityThread.main(ActivityThread.java:5017) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
    at dalvik.system.NativeStart.main(Native Method)

  

A: I got your error, as you have declare two time buffer reader therefore it is showing null pointer exception, 
Just go through my below edited code it will work
  public void readData(View view)
  {
     BufferedReader bufferedReader = null;
     StringBuilder result = new StringBuilder();
     try {
         FileInputStream fileInputStream = openFileInput("testFile");
         bufferReader = new BufferedReader(new InputStreamReader(fileInputStream));
         String line;
          while ((line=bufferReader.readLine())!=null){
              result.append(line + "\r\n");
         }
      } catch (FileNotFoundException e) {
        e.printStackTrace();
      } catch (IOException e){
        e.printStackTrace();
     }finally {
        try {
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    ((EditText)this.findViewById(R.id.editText2)).setText(result);
}


A: The problem is with the editText2, you haven't initialized it. I prefer you to do the following:

*

*Declare a private field private EditText editText;


*Bind the view (editText2) inside onCreate() method like this
editText = (EditText) findViewById(R.id.editText2);


*Inside the readData() instead of ((EditText)this.findViewById(R.id.editText2)).setText(result); use this line of code:
this.editText.setText(result);
