0

I've been trying to use the following code to export contacts as a vcf file:

b5.setOnClickListener(new 

OnClickListener()
{

public void onClick(View v){
getVCF();
}


public static void getVCF() 

{

final String vfile = "POContactsRestore.vcf";

Cursor phones = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.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();
 }

 }
 }

The problem is that this throws me a "nullpointerexception" and honestly I don't really understand the debugger. Please help me.

How can I fix it?

EDIT1:

I'm not sure of what exactly should say the Logcat, but all errors look like this:

com.android.exchange   Strict Mode      null
com.android.exchange   Strict Mode      android.app.ServiceConnectionLeaked: Service com.android exchange

The Debug window highlights this line:

Cursor phones = mContext.genContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

Thanks for your help.

EDIT2:

OK debug window says:

Thread[<1>main](Suspended (exception NullPointerException))
MainActivity$1.getVCF()line:71
MainActivity$1.onClick(View)line:63

The LogCat window does not show anything but the errors I've posted before.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • post the LogCat with the exception.... – David M Jan 23 '13 at 18:45
  • well, you say you are getting a NullPointerException. we need to see that part of the Log. It'll state exactly where the NPE occurred (the line number). – David M Jan 23 '13 at 19:16
  • I've edited my answer. I could not find any errors containing the "NullpointerException" in the LogCat. The exception just appears in Debug Window. –  Jan 23 '13 at 19:29
  • ok...can you somehow mark line 71 in the `getVCF()` function? that's where the error is occurring. – David M Jan 23 '13 at 19:34
  • There are no "QuickFix" options or something and it seems there is no error when saving it. It just launch the error once running. Im lost here, I've been trying the whole day to make it work. Thanks for your time, I thought the problem will show up by posting the code here, but it seems the code is OK. It may be something else. –  Jan 23 '13 at 19:49
  • Hey! Thanks again. Just to let you know I solved the problem. –  Jan 24 '13 at 14:25

1 Answers1

0

(Answered in a question edit. Converted to a community wiki answer. See What is the appropriate action when the answer to a question is added to the question itself? )

The OP wrote:

Just to let you know how I fixed it; I just got rid of the "static" tag in Context:

public class MainActivity extends Activity
{
 static Context mContext;

for

public class MainActivity extends Activity
{
 Context mContext;

It took me all day find out what was the problem.

Community
  • 1
  • 1
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129