Question

I don't know the reason why the same code runs OK on Android 2.3 but when it comes to android 3.1 tablet, it throws NullPointerExceptions...here's the code:

public class CalleeButton extends Button implements OnClickListener, AddressAware {
private AddressText mAddress;
private OnClickListener externalClickListener;
public void setExternalClickListener(OnClickListener e) {externalClickListener = e;}
public void setAddressWidget(AddressText a) {a=mAddress;}

//set Uri SipUri ,so we can initialize the mAddress inside calleebutton --smartclassroom
//because every calleebutton will have one sip account/ext. this will replace the information for original mAddress --smartclassroom
public void setSip(String StudentName,String SipUri, AddressText a)
{
    Log.i("CalleeButton-35");
    super.setText(StudentName);
    //mAddress.setText(SipUri);
    Log.i("CalleeButton-38");
    mAddress.setContactAddress(SipUri, StudentName);
    Log.i("CalleeButton-40");
    setAddressWidget(a);
    Log.i("CalleeButton-42");

}

public CalleeButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    setOnClickListener(this);

    //must use this line or you got null pointer exception...smartclassroom
    Log.i("CalleeButton-50");
    mAddress = new AddressText(context, attrs);
    //mAddress = null;
    Log.i("CalleeButton-52");
}

public void onClick(View v) {
    try {
        if (!LinphoneManager.getInstance().acceptCallIfIncomingPending()) {
            if (mAddress.getText().length() >0) { 
            LinphoneManager.getInstance().newOutgoingCall(mAddress);
                //add new action here
                /*******************************************************/
                /*******************************************************/
            }
        }
    } catch (LinphoneCoreException e) {
        LinphoneManager.getInstance().terminateCall();
        onWrongDestinationAddress();
    };

    if (externalClickListener != null) externalClickListener.onClick(v);
}


protected void onWrongDestinationAddress() {
    Toast toast = Toast.makeText(getContext()
            ,String.format(getResources().getString(R.string.warning_wrong_destination_address),mAddress.getText().toString())
            ,Toast.LENGTH_LONG);
    toast.show();
}


}

Actually I got NullPointerException on Android 2.3 Platform once, but after adding:

mAddress = new AddressText(context, attrs);

then it compiles and runs smoothly...until I try to install the same program on android 3.1 tablet...weird, isn't it ? Please let me learn what mistakes I have made on this, thanks in advance! the error msgs for logcat are:

04-25 23:58:13.308: ERROR/AndroidRuntime(6728): FATAL EXCEPTION: main
04-25 23:58:13.308: ERROR/AndroidRuntime(6728): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.linphone/org.linphone.LinphoneActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{org.linphone/org.linphone.DialerActivity}: java.lang.NullPointerException
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1751)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1767)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.app.ActivityThread.access$1500(ActivityThread.java:122)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1005)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.os.Looper.loop(Looper.java:132)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.app.ActivityThread.main(ActivityThread.java:4028)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at java.lang.reflect.Method.invokeNative(Native Method)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at java.lang.reflect.Method.invoke(Method.java:491)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at dalvik.system.NativeStart.main(Native Method)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{org.linphone/org.linphone.DialerActivity}: java.lang.NullPointerException
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1751)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1592)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:130)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:342)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:676)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.widget.TabHost.setCurrentTab(TabHost.java:345)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.widget.TabHost.setCurrentTabByTag(TabHost.java:282)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at org.linphone.LinphoneActivity.gotToDialer(LinphoneActivity.java:481)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at org.linphone.LinphoneActivity.fillTabHost(LinphoneActivity.java:207)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at org.linphone.LinphoneActivity.onCreate(LinphoneActivity.java:126)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1715)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     ... 11 more
04-25 23:58:13.308: ERROR/AndroidRuntime(6728): Caused by: java.lang.NullPointerException
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at org.linphone.DialerActivity.onCreate(DialerActivity.java:128)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1715)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     ... 22 more 

From my opinion,the logcat msgs told me about the function:

    mCallee1.setSip("Jack","sip:xxx@xxx.xxx.xxx.xxx",mAddress);

in DailerActivity.java(which you can treat it as main function here) caused the NullPointerExcetion, then I trace the code to CalleeButton.java, but still can't get find the point where caused the null pointer exception...

Was it helpful?

Solution

Set a breakpoint on that line (setSip()), and check the mCallee1 and mAddress variables. One should be null. That will be your problem. Then just trace it back and find out why it isn't being instantiated -- or if null is a valid state for the variable, just check it for null before calling setSip().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top