Q:

Trying to check if textfile with same name has already been created

I am trying to create a basic app where I input text to the app, this text names a text file, then the app adds extra text to the file. If a text file with the same name already exists, I want to output "file exists". At the moment from what I can see, the check to see if a file with the same name already exists is not working. Can anybody see why? From what I see it should work. Here is the code:
package com.example.user.filetest;

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.File;

public class MainActivity extends AppCompatActivity {

FileUtility myFile = new FileUtility();
private File root;
private File file;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    final EditText enter = ((EditText) findViewById(R.id.editText));
    final TextView show = ((TextView) findViewById(R.id.textView));
    Button b = ((Button) findViewById(R.id.button));
    b.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
             final String s = enter.getText().toString();
             file = new File(root, "//" + s);
             if (file.exists()) {
                 show.setText("File Exists");
             }
             else {
                 myFile.createFile(getApplicationContext(), s);
                 myFile.writeLine("test");
                 show.setText(myFile.readAll());
             }
         }
     }
    );
}

 package com.example.user.filetest;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.content.*;

public class FileUtility {

private File root;
private File file;

public FileUtility() {
    root = Environment.getExternalStorageDirectory();
}

public void createFile(Context context, String fileName) {
    try {
        if (root.canWrite()) {
            file = new File(root, "//" + fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
        }
        else
        {
            file = new File(context.getFilesDir(), "//" + fileName); // File(root, "//" + fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
        }
    } catch (IOException e) {
        Log.e("Error", "fail to create a new file");
    }

}

public String readAll() {
    StringBuilder returnString = new StringBuilder();
    try {
        BufferedReader in;
        FileReader datawriter = new FileReader(file);
        in = new BufferedReader(datawriter);
        if (file.exists()) {
            String str = null;
            while((str=in.readLine())!=null)
            {
                returnString.append(str + "\n");
            }
        }
        in.close();
    } catch (IOException e) {
        Log.e("Error", "fail to write file");
    }
    return returnString.toString();
}

public void writeLine(String message) {
    try {
        BufferedWriter out;
        FileWriter datawriter = new FileWriter(file,true);
        out = new BufferedWriter(datawriter);
        if (file.exists()) {
            out.write(message + "\n");
            out.flush();
        }
        out.close();
    } catch (IOException e) {
        Log.e("Error", "fail to write file");
    }
}

}

A:

You have an issue in the way you declare and initialize File root in both classes.
In MainActivity :
The File root attribute is not initialized 
Change your code to 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    root = Environment.getExternalStorageDirectory(); // Initialilze the root file here
    // ...

     b.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
             final String s = enter.getText().toString();
             file = new File(root, "//" + s);
             if (file.exists()) {
                 show.setText("File Exists");
             }
             else {
                 myFile.createFile(root, getApplicationContext(), s);  // pass the root file as parameter
                 myFile.writeLine("test");
                 show.setText(myFile.readAll());
             }
         }
     }
);
}

In FileUtility :
As you are also using the root in FileUtility to create the new file you can pass it as parameter and then remove the class attribute .
class FileUtility{

   private File file;

   public FileUtility() {
   }

   public void createFile(File root, Context context, String fileName){
      // Use the root initialized into the main activity 
      //...

And the you 

