-4
public class MainActivity extends AppCompatActivity {

Cursor cursor;
ArrayList<String> vCard;
String vfile;
static Context mContext;

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

    getVCF();
}

public static void getVCF() {
    final String vfile = "Contacts.vcf";

    Cursor phones = mContext.getContentResolver().query(    // exception here at this line
            ContactsContract.Contacts.CONTENT_URI, null,  
            null, null, null);
    phones.moveToFirst();
    for (int i = 0; i < phones.getCount(); i++) {
        String lookupKey = phones.getString(phones
                .getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
        Uri uri = Uri.withAppendedPath(
                ContactsContract.Contacts.CONTENT_VCARD_URI,
                lookupKey);
        AssetFileDescriptor fd;
        try {
            fd = mContext.getContentResolver().openAssetFileDescriptor(uri, "r");
            FileInputStream fis = fd.createInputStream();
            byte[] buf = new byte[(int) fd.getDeclaredLength()];
            fis.read(buf);
            String VCard = new String(buf);
            String path = Environment.getExternalStorageDirectory()
                    .toString() + File.separator + vfile;
            FileOutputStream mFileOutputStream = new FileOutputStream(path,
                    true);
            mFileOutputStream.write(VCard.toString().getBytes());
            phones.moveToNext();
            Log.d("Vcard", VCard);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
}
}

error

    FATAL EXCEPTION: main  Process: com.example..myapplication, P  
    java.lang.RuntimeException: Unable to start activity 
 ComponentInfo{com.example..myapplication/com.example.myapplication.MainActivity
  }: java.lang.NullPointerException: Attempt to invoke virtual method 
  'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference                                                                                     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object referenc   at com.example..myapplication.MainActivity.getVCF(MainActivity.java:36at com.example..myapplication.MainActivity.onCreate(MainActivity.java:30)
duggu
  • 37,851
  • 12
  • 116
  • 113
Randroid
  • 3,688
  • 5
  • 30
  • 55
  • 2
    You've not set `mContext`. Just use `getContentResolver().query(...)`. Why do you need to hold a static instance of `Context` anyway? – Michael Dodd Feb 08 '18 at 16:55

3 Answers3

1

You getting NPE because your context is null or you can say like your not initialise context. So need to set context or pass reference into context variable.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mContext = MainActivity.this;
    getVCF();
}
duggu
  • 37,851
  • 12
  • 116
  • 113
0

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object referenc at com.example..myapplication.MainActivity.getVCF(MainActivity.java:36at com.example..myapplication.MainActivity.onCreate(MainActivity.java:30)

Your exception clearly telling that mContext is null at this line:

Cursor phones = mContext.getContentResolver().query(    // exception here at this line
            ContactsContract.Contacts.CONTENT_URI, null,  
            null, null, null);

Add this line in onCreate before calling getVCF();

mContext = MainActivity.this;

OR simply remove calling getContentResolver() with mContext simply use it like:

getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null);
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
0

Since you have the method inside a activity, you dont have to use context. you can directly access ContentResolver

public static void getVCF() {
        final String vfile = "Contacts.vcf";

        Cursor phones = getContentResolver().query(    // exception here at this line
                ContactsContract.Contacts.CONTENT_URI, null,  
                null, null, null);
        phones.moveToFirst();
        for (int i = 0; i < phones.getCount(); i++) {
            String lookupKey = phones.getString(phones
                    .getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
            Uri uri = Uri.withAppendedPath(
                    ContactsContract.Contacts.CONTENT_VCARD_URI,
                    lookupKey);
            AssetFileDescriptor fd;
            try {
                fd = getContentResolver().openAssetFileDescriptor(uri, "r");
                FileInputStream fis = fd.createInputStream();
                byte[] buf = new byte[(int) fd.getDeclaredLength()];
                fis.read(buf);
                String VCard = new String(buf);
                String path = Environment.getExternalStorageDirectory()
                        .toString() + File.separator + vfile;
                FileOutputStream mFileOutputStream = new FileOutputStream(path,
                        true);
                mFileOutputStream.write(VCard.toString().getBytes());
                phones.moveToNext();
                Log.d("Vcard", VCard);
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }
    }
Sivakumar S
  • 681
  • 5
  • 19