I am developing an Android app for interepting calls. The call intercepting part works fine! but then when I try to save the call number into a file, the app crashes. I am not a pro in Android programming.

This is my Java classes: 

```
private class CallStateListener extends PhoneStateListener {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
        switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                // called when someone is ringing to this phone
                Toast.makeText(ctx,"Incoming: "+incomingNumber,
                        Toast.LENGTH_LONG).show();
               ob.write(incomingNumber);
                break;
        }

```

ob is an object of MainActivity. write() is a public method in MainActivity. My MainActivity is as below: 
public class MainActivity extends Activity {

```
private boolean detectEnabled;

private TextView textViewDetectState;
private Button buttonToggleDetect;
private Button buttonExit;

public void write(String a) {

    try {
       File sdcard=getExternalFilesDir(null);
        File directory=new File(sdcard.getAbsolutePath()+"/MyFiles");
        directory.mkdir();
        File file=new File(directory,"CALLS.txt");
        FileOutputStream  fout=new FileOutputStream(file);
        OutputStreamWriter osw=new OutputStreamWriter(fout);
        osw.append(a);
        osw.close();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    catch (NullPointerException e) {
        e.printStackTrace();
    }

```