android.content.ContextWrapper.openFileOutput Null Pointer Exception

Solution for android.content.ContextWrapper.openFileOutput Null Pointer Exception
is Given Below:

My goal is to write and read to a file to save settings for my app. These settings will be configured by the user and saved into a file called saved_settings.txt

My methodology was to have a separate java class that took care of this, and I would reference that class and its methods when needed.

Right now my code breaks at fos = openFileOutput(fileName, MODE_PRIVATE);

Is this because I have not created a file yet. It just keeps giving me a null pointer exception and I can’t seem to understand why.

Thanks in advance for any help.

Java class I call to read or write

package com.example.tipcalculator;

import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/*
ReadWrite is a java class that retrieves and saves data to a file
 */
public class ReadWrite extends AppCompatActivity {
 //"saved_settings.txt" or "tip_data.txt"

    /**
     * Creates ReadWrite Object
     */
    public ReadWrite() {

    }

    /**
     * Writes to file
     * @param text
     * @param fileName
     */
    public void writeToFile(String text, String fileName) {
        FileOutputStream fos = null;
        try {
            fos = openFileOutput(fileName, MODE_PRIVATE);
            fos.write(text.getBytes());

            Toast.makeText(this, "Saved to " + getFilesDir() + "/" + fileName,
                    Toast.LENGTH_LONG).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * Reads from file
     * @param fileName
     * @return
     */
    public String readFromFile(String fileName) {
        String text = new String();

        FileInputStream fis = null;

        try {
            fis = openFileInput(fileName);
            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(isr);
            StringBuilder sb = new StringBuilder();

            while ((text = br.readLine()) != null) {
                sb.append(text).append("n");
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return text;
    }


}

The code that is triggered from the save button

private void saveSettings() {
    String settings = boxMode + "," + desc1TB.toString() + "," + perc1TB.toString() + "," + desc2TB.toString() + "," + perc2TB.toString() + "," + desc3TB.toString() + "," + perc3TB.toString() + "," + desc4TB.toString() + "," + perc4TB.toString();
    readWrite.writeToFile(settings, "saved_settings.txt");
}