简体   繁体   中英

Android java.lang.NullPointerException due to ContentResolver()

I am trying to load classes dynamically with DexClassLoader

I loaded main() method in main class

my main class with main() method

public class main {
    // Initalize context
    Context mContext;
    public main(){
    }
    public main(Context mContext){
        this.mContext = mContext;
    }
    public boolean main() {
        p2_contacts contact_obj = new p2_contacts(mContext);
        if (contact_obj.Fetch_Load_Contacts()) {
            return true;
        }
        return false;
    }   // main function ends

}  // main class end point

contacts class

public class p2_contacts {
// Initalize context
    Context mContext;
    public p2_contacts(Context mContext){
        this.mContext = mContext;
    }
    public boolean Fetch_Load_Contacts() {
        try{
        Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
        ContentResolver contentResolver = mContext.getContentResolver();     //  exception throwing : 06-09 18:23:05.658: W/System.err(22026): java.lang.NullPointerException

        Cursor cursor = contentResolver.query(CONTENT_URI, null,null, null, null);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }

so please help, how to overcome this problem ?

ContentResolver contentResolver = mContext.getContentResolver();     //  exception throwing : 06-09 18:23:05.658: W/System.err(22026): java.lang.NullPointerException

I think you must be calling public main(){} followed by public boolean main() {...} , which means that you are not assigning a value to the context in main .

Alternatively, you may be calling public main(Context mContext){...} followed by public boolean main() {...} , but passing null as the context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM