0

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
Vivek Warde
  • 1,936
  • 8
  • 47
  • 77

1 Answers1

0

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.

JamesK
  • 164
  • 8
  • so how can i get context for the separate class public class main or public class p2_contacts because i can't pass context from my activity to them because am loading class public main with dex loader as it's separate android library project . – user134929 Jun 10 '14 at 05:10
  • for example : final Class classToLoad = (Class) classloader.loadClass(pack); // package plus main class name // so how can i pass context with .loadclass(pack) !!!! or any separate way to declare context for that external library which am loading – user134929 Jun 10 '14 at 05:16