Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I wrote a method in MainActivity and when i call it from same activity (main) it run correctly but when i call it from another class i get this error
i write this program for android.

Logcat output:
04-22 19:53:11.895  16739-16739/mohammadi.behnam.lstcall I/##Contactadder##﹕ +989333346529
04-22 19:53:11.895  16739-16739/mohammadi.behnam.lstcall D/AndroidRuntime﹕ Shutting down VM
04-22 19:53:11.895  16739-16739/mohammadi.behnam.lstcall W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41d0bc08)
04-22 19:53:11.895  16739-16739/mohammadi.behnam.lstcall E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: mohammadi.behnam.lstcall, PID: 16739
    java.lang.NullPointerException
            at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:105)
            at mohammadi.behnam.lstcall.MainActivity.Contactadder(MainActivity.java:236)
            at mohammadi.behnam.lstcall.IncomingCall$MyPhoneStateListener.onCallStateChanged(IncomingCall.java:57)
            at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:461)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5635)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)


Java
public void Contactadder(String incomingNumber)
    {
    
        Log.i("##Contactadder##", incomingNumber);
        String RawcID="";

        String[] projectionPhones = {
                ContactsContract.Data.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
                ContactsContract.CommonDataKinds.Phone.NUMBER,
                ContactsContract.CommonDataKinds.Phone.TYPE,
                ContactsContract.CommonDataKinds.Phone.LABEL
        };


        ContentResolver cr = this.getContentResolver();

        Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projectionPhones,
                ContactsContract.CommonDataKinds.Phone.NUMBER +"=?", new String[]{incomingNumber}, null);

        while(pCur.moveToNext())
        {
            RawcID=pCur.getString(1);
        }


   
        ContentValues values = new ContentValues();

        values.clear();
        values.put(ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID, RawcID);
   

        getContentResolver().insert(ContactsContract.Data.CONTENT_URI
                , values);

    }

service class that i wanna call that method:
Java
MainActivity ma=new MainActivity();
ma.Contactadder("+9183331111");
Posted
Updated 22-Apr-15 7:23am
v2
Comments
Sergey Alexandrovich Kryukov 22-Apr-15 12:32pm    
This is way too easy to investigate. You are given the line number where the exception is thrown. Use the debugger and stop at the previous line, examine what is not supposed to be null but is actually null.
—SA

Debugging[^]

and add a break point for the Nullpointer exception.

Have fun!
 
Share this answer
 
i solved it by using singleton pattern
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900