Skip to content
This repository has been archived by the owner on Mar 16, 2021. It is now read-only.

java.lang.NullPointerException: Attempt to invoke interface method #12

Closed
k0shk0sh opened this issue Sep 20, 2016 · 2 comments
Closed
Labels

Comments

@k0shk0sh
Copy link
Contributor

k0shk0sh commented Sep 20, 2016

as per the title, calling getPresenter() on Activity onCreate causes this crash for example

@Override protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   if (savedInstanceState == null) {
      getPresenter().onSubmit(getIntent());
   }
}
if (intent == null || intent.getExtras() == null) {
    getView().onShowError(R.string.general_error);
}

any idea why would this happen? why wouldn't the view being initialised onCreate?

@passsy
Copy link
Contributor

passsy commented Sep 20, 2016

To be clear, you are calling getPresenter().onSubmit(getIntent()); and the implementation of #onSubmit(Intent) calls TiPresenter#getView(). TiPresenter#getView() is null and causes the NPE.

If so, everything is correct. TiPresenter#getView() is null until TiPresenter#onWakeUp() has been called (which will be called internally after Activity#onStart()).
I assume #onSubmit(Intent) is in TiActivity#onCreate() for testing and should normally be triggered from a Button. This will work, because the Button can only be clicked when the view is visible to the user. At that point, TiPresenter#onWakeUp() already got called and TiPresenter#getView() will not be null anymore.

When you really want to call the TiPresenter in TiActivity#onCreate() you have to make sure you don't trigger the view when it is not attached. Instead cache data in you Presenter and wait until the view is attached.

You are using if (savedInstanceState == null) which indicates you want some kind of initialization when the activity is initialized for the first time. Why aren't you moving that logic into TiPresenter#onCreate()?
You can pass the Intent into the constructor of your Presenter in TiActivity#providePresenter().

@k0shk0sh
Copy link
Contributor Author

seems pretty legit to actually pass the intent to the presenter constructor. i got no idea why wouldn't i thought of it. Thank you for the explanation and your answer.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

2 participants