Yankee Yankee - 1 year ago 78
Android Question

Android - Can't find the reason for NullPointerException

I am trying to integrate Sinch video calling into my app, so I just copied all those Activities and Layouts from the Sinch Video-Call Demo App and it worked fine. Now I am trying to remove the

so that a call establishes using a default, hard-coded
instead of getting it from an
. And I only made changes to the
layout files.

But now, as soon as that Activity launches, it crashes with this error.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.yankee.cw/com.example.yankee.cw.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setEnabled(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2462)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2522)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5471)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setEnabled(boolean)' on a null object reference

But nowhere in the whole code there is a
called on any
object. Here is a link to the original layout file
. And here is mine
. Original
and mine

I am sorry this might seem like a stupid question but I have tried everything I could in the code for the last 5 hours and I still couldn't find what is causing the problem because there is no call to
anywhere in the entire code. Thank you.

Answer Source

After reading your modifications and the base repository of the project in question I think the reason for the NullPointerException is the following:

After starting your LoginActivity it immediately follows the execution with LoginActivity#loginClicked() which starts the next activity rights away

private void openPlaceCallActivity() {
    Intent mainActivity = new Intent(this, PlaceCallActivity.class);

... at android.app.ActivityThread.performLaunchActivity ...

The PlaceCallActivity extends BaseActivity which modifies the service lifecycle thus maybe calling the PlaceCallActivity#onServiceConnected before PlaceCallActivity#onCreate... which coincidentally may have a not initialized mCallButton


        protected void onServiceConnected() {
            TextView userName = (TextView) findViewById(R.id.loggedInName);
            mCallButton.setEnabled(true);  // <--- might try changing this?
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download