Yankee Yankee - 1 month ago 12
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

LoginActivity
so that a call establishes using a default, hard-coded
userName
instead of getting it from an
EditText
. And I only made changes to the
LoginActivity.java
and
login.xml
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
setEnabled
called on any
Button
object. Here is a link to the original layout file
login.xml
. And here is mine
login.xml
. Original
LoginActivity.java
and mine
LoginActivity.java


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
setEnabled
anywhere in the entire code. Thank you.

Answer

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);
    startActivity(mainActivity);
}

... 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

PlaceCallActivity#onServiceConnected

    @Override
        protected void onServiceConnected() {
            TextView userName = (TextView) findViewById(R.id.loggedInName);
            userName.setText(getSinchServiceInterface().getUserName());
            mCallButton.setEnabled(true);  // <--- might try changing this?
        }