Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException calling Flow.get(this) in the onCreate method. #211

Closed
epool opened this issue Sep 14, 2016 · 9 comments
Closed

NullPointerException calling Flow.get(this) in the onCreate method. #211

epool opened this issue Sep 14, 2016 · 9 comments
Assignees
Labels
Milestone

Comments

@epool
Copy link

epool commented Sep 14, 2016

I am getting the following error when I call Flow.get(this) inside the onCreate() method.

Caused by: java.lang.NullPointerException: Attempt to read from field 'flow.Flow flow.InternalLifecycleIntegration.flow' on a null object reference
                                                     at flow.InternalContextWrapper.getSystemService(InternalContextWrapper.java:54)
                                                     at android.view.ContextThemeWrapper.getSystemService(ContextThemeWrapper.java:123)
                                                     at android.app.Activity.getSystemService(Activity.java:5263)
                                                     at flow.InternalContextWrapper.getFlow(InternalContextWrapper.java:31)
                                                     at flow.Flow.get(Flow.java:46)
                                                     at flow.sample.basic.BasicSampleActivity.onCreate(BasicSampleActivity.java:30)
                                                     at android.app.Activity.performCreate(Activity.java:6237)
                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                     at android.os.Looper.loop(Looper.java:148) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                     at java.lang.reflect.Method.invoke(Native Method)

The code:

public class BasicSampleActivity extends Activity {

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

    Flow flow = Flow.get(this); // <============== ERROR!!! NPE
    System.out.println(flow);
  }

  @Override protected void attachBaseContext(Context baseContext) {
    baseContext = Flow.configure(baseContext, this) //
        .dispatcher(new BasicDispatcher(this)) //
        .defaultKey(new WelcomeScreen()) //
        .keyParceler(new BasicKeyParceler()) //
        .install();
    super.attachBaseContext(baseContext);
  }

  @Override public void onBackPressed() {
    if (!Flow.get(this).goBack()) {
      super.onBackPressed();
    }
  }
}

Somebody can explain me why this is happening in the onCreate() method? If I do the same in onStart() or onResume() everything works well.

I am not sure if this is a bug or not, but I think a wiki/documentation is required for this awesome library.

@Zhuinden
Copy link

It would work with Flow.get(getBaseContext())

@epool
Copy link
Author

epool commented Sep 16, 2016

@Zhuinden It did not work for me =/ still crashing with NPE Caused by: java.lang.NullPointerException: Attempt to read from field 'flow.Flow flow.InternalLifecycleIntegration.flow' on a null object reference.

@Zhuinden
Copy link

Zhuinden commented Sep 16, 2016

@epool you might be correct, Flow is initialized in onActivityCreated() method of InternalLifecycleIntegration (retained fragment), so you might be better off if you move that logic to onPostCreate()

@epool
Copy link
Author

epool commented Sep 16, 2016

@Zhuinden That worked, Thanks!

@rjrjr
Copy link
Collaborator

rjrjr commented Apr 13, 2017

I don't know if the behavior can change, but we should be able to do better than throwing an NPE. Plan is to document that it is unsafe to call Flow#get(Context) before the first call to Activity#onResume().

@rjrjr
Copy link
Collaborator

rjrjr commented Apr 13, 2017

Also, very curious to know why people are trying to get their hands on the Flow instance before that point.

@Zhuinden
Copy link

Go to login or go to feed in onCreate?

@rjrjr
Copy link
Collaborator

rjrjr commented Apr 13, 2017

Why does that need to happen in onCreate rather than onResume? Or why not calculate the default key to be login or feed before kicking things off?

Actually, I think the best place to handle that kind of ACL concern is in the dispatcher itself. On each dispatch, if auth has been lost, redirect to the login screen instead of honoring the dispatch. Something like:

@Override public void dispatch(Traversal traversal, TraversalCallback callback) {
  if (!authorizer.readyFreddy()) {
    getFlow().setHistory(History.single(LoginScreen.INSTANCE));
    callback.onTraversalCompleted();
    return;
  }

  // etc.
}

@loganj
Copy link
Collaborator

loganj commented Apr 14, 2017

+1 to handling this in the dispatcher.

@rjrjr rjrjr self-assigned this Apr 14, 2017
rjrjr added a commit that referenced this issue Apr 14, 2017
In reponse to the confusion expressed in #211.
rjrjr added a commit that referenced this issue Apr 14, 2017
In reponse to the confusion expressed in #211.
rjrjr added a commit that referenced this issue Apr 14, 2017
In reponse to the confusion expressed in #211.
@rjrjr rjrjr closed this as completed Apr 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants