question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
Zhuindencommented, 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()

0reactions
loganjcommented, Apr 14, 2017

+1 to handling this in the dispatcher.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NullPointerException accessing views in onCreate()
I get NullPointerException when attempting to call a method on View s obtained with findViewById() in my activity onCreate() .
Read more >
Getting Error: java.lang.NullPointerException in t... - ServiceNow
Hi, I created a catalog items with two choice fields and one is dependent on another. 1. category -- values are database and...
Read more >
Null Safety Tutorial in Kotlin: Best Practices - Kodeco
In this tutorial, you'll look at Kotlin's nullability best practices. You'll learn about null safety in Kotlin and how to avoid NPEs.
Read more >
Handling Lifecycles with Lifecycle-Aware Components
A common pattern is to implement the actions of the dependent components in the lifecycle methods of activities and fragments.
Read more >
Null Pointer Exception In Method (Android forum at Coderanch)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean org.json.JSONObject.getBoolean(java.lang.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found