Question

java.lang.NullPointerException:

  • 26 November 2022
  • 2 replies
  • 229 views

Badge +5

I am getting the following error with the code below: ‘java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.revenuecat.purchases.EntitlementInfo.isActive()' on a null object reference’

 

Purchases.getSharedInstance().getCustomerInfo(new ReceiveCustomerInfoCallback() {
@Override
public void onReceived(@NonNull CustomerInfo customerInfo) {
Log.i("RevenueCat", String.valueOf(customerInfo.getEntitlements()));
Log.i("RevenueCat", String.valueOf(customerInfo.getEntitlements().getAll()));
if (customerInfo.getEntitlements().get(premiumEntitlementID).isActive()) {
sharedPreferences.edit().putBoolean("premium", true).apply();
}
else {
sharedPreferences.edit().putBoolean("premium", false).apply();
}
}

@Override
public void onError(@NonNull PurchasesError purchasesError) {
Log.i("RC error", String.valueOf(purchasesError));
}
});

 

.getEntitlements() is returning an empty dictionary too.

 

SDK version 5.4.0

 

Please could someone help me resolve this?


2 replies

Userlevel 5
Badge +9

I don’t have a lot of information to use here but it seems like this code line might be failing

customerInfo.getEntitlements().get(premiumEntitlementID).isActive()

I think Java is complaining that the result of .get(premiumEntitlementID) is null so you can’t call isActive() on it. Java should tell you the line number of the error. Can you try using the exact string instead of premiumEntitlementID? Also make sure you spelled the entitlement ID correctly in your app and in the dashboard.

Userlevel 1
Badge +5

Hi -

Not sure if this will help, but I was running into this as well with the get(ENTITLEMENT_ID).isactive()

I believe mine is being caused by a new user that has never had the entitlement.

 

To get around the null pointer exception, i added the following ‘if’ before checking isActive()

(customerInfo.getEntitlements() != null && customerInfo.getEntitlements.get(ENTITLEMENT_ID) != null)

 

I hope that this helps

Reply