Question

PurchasePackage() throws NullReferenceException

  • 19 March 2024
  • 4 replies
  • 100 views

Badge

I pass the Package that I got from Purchases.GetOfferings() to Purchases.PurchasePackage().

 

2024/03/18 17:13:58.628 6919 6999 Error Unity NullReferenceException: Object reference not set to an instance of an object.
2024/03/18 17:13:58.628 6919 6999 Error Unity   at RevenueCat.SimpleJSON.JSONNode.Escape (System.String aText) [0x00000] in <00000000000000000000000000000000>:0 
2024/03/18 17:13:58.628 6919 6999 Error Unity   at RevenueCat.SimpleJSON.JSONString.WriteToStringBuilder (System.Text.StringBuilder aSB, System.Int32 aIndent, System.Int32 aIndentInc, RevenueCat.SimpleJSON.JSONTextMode aMode) [0x00000] in <00000000000000000000000000000000>:0 
2024/03/18 17:13:58.628 6919 6999 Error Unity   at RevenueCat.SimpleJSON.JSONObject.WriteToStringBuilder (System.Text.StringBuilder aSB, System.Int32 aIndent, System.Int32 aIndentInc, RevenueCat.SimpleJSON.JSONTextMode aMode) [0x00000] in <00000000000000000000000000000000>:0 
2024/03/18 17:13:58.628 6919 6999 Error Unity   at RevenueCat.SimpleJSON.JSONObject.WriteToStringBuilder (System.Text.StringBuilder aSB, System.Int32 aIndent, System.Int32 aIndentInc, RevenueCat.SimpleJSON.JSONTextMode aMode) [0x00000] in <00000000000000000000000000000000>:0 
2024/03/18 17:13:58.628 6919 6999 Error Unity   at RevenueCat.SimpleJSON.JSONNode.ToString () [0x00000] in <00000000000000000000000000000000>:0 
2024/03/18 17:13:58.628 6919 6999 Error Unity   at PurchasesWrapperAndroid.Purchase

 

Unity SDK v6.9.0

Unity 2020.3.30f1

App is running on my phone ran directly from Unity via USB.  Error above from logcat.


This post has been closed for comments

4 replies

Userlevel 4
Badge +8

This error is usually caused when an object is trying to be used by a script but does not refer to an instance of an object. Unity has a help article on this here, can you please check your code to see if this solution can help?  https://support.unity.com/hc/en-us/articles/206369473-NullReferenceException

Badge

@Haley Pace If you look at the stack trace you can see that everything is in the RevenueCat SDK.  In fact, I am calling PurchasePackage() on an object that was given to me by the GetOfferings() call.  

I know what a Null reference is, and I am not passing a null reference.  Here is my code:

      if ( package == null ) {
return;
}

Debug.Log( "Package: "+ package );

Purchases.PurchasePackage(package, (identifier, info, cancelled, error) => {

It seems to me that something in the RevenueCat SDK is attempting to serialize to JSON for the API call for the purchase, and a field somewhere in the Package object tree is null.  Is there some configuration in RevenueCat somewhere that is missing that causes a null value?  I may be able to fix that if someone would just tell me what it is … but regardless, the SDK should handle it.

I don’t want to post the contents of the Debug.Log() output here, but package is definitely not null.

 

Badge

@Haley Pace @Tavis Elliott  In my case after debugging I found out that TargetingContext.RuleId was null which resulted in above error.
So I changed this condition in PresentedOfferingContext.ToJsonString() method

if (TargetingContext != null)

to this

if (TargetingContext != null && !string.IsNullOrEmpty(TargetingContext.RuleId))


Now it works.

Badge

@Kaushik Ah, thanks for the insight!  I’m loathe to copy and modify an external SDK, but if that’s what I have to do for now …  I took a different approach, and made these modifications:


In JSONString#WriteToStringBuilder() I changed this:

            aSB.Append('\"').Append(Escape(m_Data)).Append('\"');

To this:

          if ( null == m_Data ) {
aSB.Append("null");
}
else {
aSB.Append('\"').Append(Escape(m_Data)).Append('\"');
}

I made similar changes in JSONObject#WriteToStringBuilder() to check for null key/value in the Dictionary, though I don’t think that was my problem.

 

@Haley Pace Any thoughts about how soon I can get a fix for this issue in the official SDK?  Conversely, is there a configuration somewhere in RevenueCat or my Google play store setup that I can change to cause the “ruleId” to not be null?