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.

Weird NullpointerException when removing fabOption

See original GitHub issue

I am doing this… private void removeFabOption(FabOption fabOption) { expandableFabLayout.getPortraitConfiguration().getFabOptions().remove(fabOption); }

and get this exception… E/AndroidRuntime: FATAL EXCEPTION: main Process: com.pezcraft.myapplication, PID: 30800 java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getVisibility()' on a null object reference at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3416) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3415) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3415) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3415) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3415) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3415) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3415) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1944) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1633) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7943) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1092) at android.view.Choreographer.doCallbacks(Choreographer.java:893) at android.view.Choreographer.doFrame(Choreographer.java:812) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1078) at android.os.Handler.handleCallback(Handler.java:907) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:216) at android.app.ActivityThread.main(ActivityThread.java:7625) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987) I/Process: Sending signal. PID: 30800 SIG: 9

But what’s weird is that it worked at first but when I started it a 2nd, 3rd, … time, I got this exception.

By the way, I am calling the function inside onBillingSetupFinished Listener. It worked perfectly outside the listener. `

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    expandableFabLayout = findViewById(R.id.expandableFabLayout);

    prefManager = new PrefManager(getApplicationContext());

    checkBox = findViewById(R.id.checkBox);
    linearLayoutHighestHeartrate = findViewById(R.id.linearLayoutHighestHeartrate);
    textViewHighestHeartrate = findViewById(R.id.textViewHighestHeartrate);

    //if it's still the same day, do not reset the highest heartrate of the day
    if(prefManager.getHighestHeartrateDate().equals(DateConverter.toDate(new Date()))) {
        highestHeartrate = prefManager.getHighestHeartrate();
    }
    textViewHighestHeartrate.setText(getString(R.string.textViewHighestHeartrate, highestHeartrate));

    //check subscription status
    billingClient = BillingClient.newBuilder(this).enablePendingPurchases().setListener(this).build();
    billingClient.startConnection(new BillingClientStateListener() {
        @Override
        public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
            if (billingResult.getResponseCode() == BillingResponseCode.OK){
                billingClient.queryPurchasesAsync(SkuType.SUBS, (billingResult1, queryPurchasesSubs) -> {
                    if (queryPurchasesSubs.size() > 0){
                        handlePurchases(queryPurchasesSubs);
                    } else {
                        //if no item in purchase list means no, cancelled, or paused subscription
                        prefManager.setPremium(false);
                        removeFabOption(findViewById(R.id.fabOptionPremium));
                    }

                    //set keeprunning-checkbox
                    if(prefManager.isPremium() && prefManager.getKeepRunning()) {
                        checkBox.setChecked(true);
                    }

                    billingClient.queryPurchasesAsync(SkuType.INAPP, (billingResult2, queryPurchasesOnetime) -> {
                        if (queryPurchasesOnetime.size() > 0){
                            handlePurchases(queryPurchasesOnetime);
                        } else {
                            //if no item in purchase list means no, cancelled, or paused subscription
                            prefManager.setPremium(false);
                            removeFabOption(findViewById(R.id.fabOptionPremium));
                        }

                        //set keeprunning-checkbox
                        if(prefManager.isPremium() && prefManager.getKeepRunning()) {
                            checkBox.setChecked(true);
                        }
                    });
                });

                //query prices for BottomSheetDialog
                getSubPrice();
                getOnetimePrice();
            }
        }

        @Override
        public void onBillingServiceDisconnected() {
            Log.d(DEBUG, "Billing Service disconnected.");
        }
    });`

Any ideas, how I can use it inside the listener?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
Pezcraftcommented, Nov 8, 2021

@kabumere Yes, it did. Thank you very much 😃

0reactions
kabumerecommented, Nov 8, 2021

Hey @Pezcraft,

Did the above solution answer your question?

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - What is a NullPointerException, and how do I fix it?
In Java 14, the following is a sample NullPointerException Exception message: ... stops throwing the exception when s.length() is removed from the method....
Read more >
Java NullPointerException - Detect, Fix, and Best Practices
Write methods returning empty objects rather than null wherever possible, for example, empty list, empty string, etc. 6. There are some methods ...
Read more >
Compatibility with Material Components · Issue #1 - GitHub
Weird NullpointerException when removing fabOption #38. Closed. Sign up for free to join this conversation on GitHub.
Read more >
Null pointer exception when right-clicking downed colonists
Hi, my savegame migrated from a few versions before and i experience an odd behaviour: When a Colonist gets shot and goes down, ......
Read more >
What could be the problem if you get NullPointerException ...
Another possible cause is that the failed event being deleted belongs to an application that has already been removed.Usually the code tries to...
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