-1

I am trying to integrate the Sinch Android SDK in my project and I wanted to remove the Login button, i.e. such that the Activity switches to the next activity without the user having to click the Login button. For this, I removed the code for setOnClickListener for the mLoginButton and instead, directly called the loginClicked() function, which was previously being called from the onClick() method.
But this causes NullPointerException: Attempt to invoke virtual method 'boolean com.example.yankee.cw.SinchService$SinchServiceInterface.isStarted()' on a null object reference.
These are the only changes I made to the code:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        mLoginName = (EditText) findViewById(R.id.loginName);

        mLoginButton = (Button) findViewById(R.id.loginButton);
        mLoginButton.setEnabled(false);
//        mLoginButton.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                loginClicked();
//            }
//        });
        loginClicked();
    }

And earlier it was like this:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        mLoginName = (EditText) findViewById(R.id.loginName);

        mLoginButton = (Button) findViewById(R.id.loginButton);
        mLoginButton.setEnabled(false);
        mLoginButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                loginClicked();
            }
        });
    }  

Why doesn't this work and gives the NullPointerException instead ?

Edit: LoginActivity.java

Yankee
  • 1,680
  • 2
  • 20
  • 40

2 Answers2

3

getSinchServiceInterface() method is returning null that is why you are getting null pointer exception.

Earlier loginCliked() method was getting called after button was enabled in onServiceConnected method.

try calling loginClicked() method in onServiceConnected() method or use some delay.

Ishan
  • 1,125
  • 10
  • 23
  • Wow, bro. I've been crying over this problem since a week. And this just worked. Thanks a lot. – Yankee Nov 02 '16 at 07:47
  • your welcome :) – Ishan Nov 02 '16 at 07:48
  • Would you please mind explaining it to me briefly why `loginClicked()` needed to be placed in the `onServiceConnected()` ? – Yankee Nov 02 '16 at 07:48
  • 1
    getSinchServiceInterface() is not getting initialized till onServiceConnected() method is called and that is why it is returning null. – Ishan Nov 02 '16 at 07:50
2

My solution :

final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
    @Override
    public void run() {
       loginClicked();
    }
}, 4000);
Mukesh Ram
  • 5,814
  • 4
  • 15
  • 34