Question: I am struck in a part of the code where an alert box is displayed asking the user to enter the name of the dataset. A file of the same name will be created in the internal storage. The alert dialog is working fine but the file created in the sd card is always "`null.txt`". After hours of searching, I have tried all the solutions such as creating a custom layout for the alert box, adding a `TextWatcher` to the `edittext` etc, but of no help. When I manually assign the name of the dataset to the variable 'Set', the file is successfully created. Thus there is a problem with the `alertdialog`. I am using android studio 2.3.1. Kindly help.

I am also posting the code.

```
else if (map2.contains("1") || map2.contains("4")) {
    final EditText edittext = new EditText(Scan.this);
    AlertDialog.Builder alert1 = new AlertDialog.Builder(Scan.this);
    alert1.setMessage("Name of the dataset");
    alert1.setTitle("Enter Your Title");
    alert1.setView(edittext);
    alert1.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            String set = edittext.getText().toString();
            Set = set;
        }
    });
    alert1.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int whichButton) {
        }
    });
    alert1.show();
}
myFile = new File(Environment.getExternalStorageDirectory(),Set+".txt");
if (!myFile.exists()) {
    try {
        myFile.createNewFile();
        FileOutputStream fOut = new FileOutputStream(myFile);
        OutputStreamWriter osw = new OutputStreamWriter(fOut);
        osw.write(message1);
        osw.flush();
        osw.close();
        fOut.close();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
   finally {
   }
}

```

Answer: 
you are creating file before set title in dialog.
try this:

```
else if (map2.contains("1") || map2.contains("4")) {
    final EditText edittext = new EditText(Scan.this);
    AlertDialog.Builder alert1 = new AlertDialog.Builder(Scan.this);
    alert1.setMessage("Name of the dataset");
    alert1.setTitle("Enter Your Title");
    alert1.setView(edittext);
    alert1.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            String set = edittext.getText().toString();
            createFile(set);
        }
    });
    alert1.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int whichButton) {
        }
    });
    alert1.show();
}
}
private void createFile(String set){
   myFile = new 
 File(Environment.getExternalStorageDirectory(),set+".txt");
   if (!myFile.exists()) {
       try {
           myFile.createNewFile();
           FileOutputStream fOut = new FileOutputStream(myFile);
           OutputStreamWriter osw = new OutputStreamWriter(fOut);
           osw.write(message1);
           osw.flush();
           osw.close();
           fOut.close();
       }
       catch (Exception e) {
           e.printStackTrace();
       }
       finally {
       }
   }

```

}
Did you give permissions in Manifest:

```
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

```

Please check this for creating `.txt` file. 

```
   String editString = mEditText.getText().toString();
   filename = fileName_mEditText.getText().toString();

   if (filename.trim().equalsIgnoreCase("")) {
       fileName_mEditText.setError("Enter File Name!");
   }else if (editString.trim().equalsIgnoreCase("")) {
       mEditText.setError("Enter Text!");
   } else {
       try {
            FileOperations fop = new FileOperations();
            fop.write(filename, editString);
            if (fop.write(filename, editString)) {
                 Toast.makeText(getApplicationContext(), filename + ".txt created", Toast.LENGTH_SHORT).show();
            } else {
                 Toast.makeText(getApplicationContext(), "I/O error", Toast.LENGTH_SHORT).show();
            }
            } catch (Exception e) {
                    e.printStackTrace();
            }
       }
  }

```