[SOLVED] Error calling Moralis.Web3Api.Account.GetNFTs

Hello,

I added the unity-web3-game-kit to my unity project, the webgl build is success, it can connect to the wallet.

Then I go back to unity editor and test the GetNFTs function, get an error:

Following is the unity function:

        public async void CheckNFT()
        {
            string userAddress = "0xb57c648b5e6992b4c58accc236c9a0979c2c08d3";
            ChainList chainId = ChainList.eth;

            NftOwnerCollection nftCollection = await Moralis.Web3Api.Account.GetNFTs(userAddress, chainId);

            if (nftCollection.Total < 1)
            {
                Debug.Log($"User {userAddress} does not have any NFTs on chain {chainId.ToString()}");
            }
            else
            {
                Debug.Log($"Nfts for User {userAddress}");

                foreach (NftOwner nft in nftCollection.Result)
                {
                    Debug.Log($"TokenId: {nft.TokenId}, Name: {nft.Name}, Balance: {nft.Amount}");
                }
            }
        }

The error message:

NullReferenceException: Object reference not set to an instance of an object
MoralisUnity.Web3Api.Client.ApiClient+<CallApi>d__12.MoveNext () (at Library/PackageCache/[email protected]/Runtime/Web3Api/Client/ApiClient.cs:97)

The error is from this code:
await Moralis.Web3Api.Account.GetNFTs(userAddress, chainId);

Is there something I missed?

Thanks a lot

maybe the result returned by that web3api call is not of type NftOwnerCollection in

Is this code has any issue?

I just copy it from unity-web3-game-kit github example:

I’m not expert in unity. We should wait for someone that knows unity better.

have you tried logging the result nftCollection before you add other code, there isnt supposed to be an error :thinking:

can you post the whole error message or is that just it ?

Hello,

I have removed all other code and just using this code:
NftOwnerCollection nftCollection = await Moralis.Web3Api.Account.GetNFTs(userAddress, chainId);

The “CheckNFT()” function will be called when I click the GetNFT button.


The error is:

NullReferenceException: Object reference not set to an instance of an object
MoralisUnity.Web3Api.Client.ApiClient+<CallApi>d__12.MoveNext () (at Library/PackageCache/[email protected]/Runtime/Web3Api/Client/ApiClient.cs:97)
--- End of stack trace from previous location where exception was thrown ---
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at <695d1cc93cca45069c528c15c9fdd749>:0)
Cysharp.Threading.Tasks.UniTask+ExceptionResultSource`1[T].GetResult (System.Int16 token) (at Library/PackageCache/[email protected]/Runtime/External/UniTask/Runtime/UniTask.Factory.cs:235)
Cysharp.Threading.Tasks.UniTask`1+Awaiter[T].GetResult () (at Library/PackageCache/[email protected]/Runtime/External/UniTask/Runtime/UniTask.cs:653)
MoralisUnity.Web3Api.CloudApi.AccountApi+<GetNFTs>d__12.MoveNext () (at Library/PackageCache/[email protected]/Runtime/Web3Api/CloudApi/AccountApi.cs:343)
--- End of stack trace from previous location where exception was thrown ---
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at <695d1cc93cca45069c528c15c9fdd749>:0)
Cysharp.Threading.Tasks.UniTask+ExceptionResultSource`1[T].GetResult (System.Int16 token) (at Library/PackageCache/[email protected]/Runtime/External/UniTask/Runtime/UniTask.Factory.cs:235)
Cysharp.Threading.Tasks.UniTask`1+Awaiter[T].GetResult () (at Library/PackageCache/[email protected]/Runtime/External/UniTask/Runtime/UniTask.cs:653)
MoralisUnity.Demos.Introduction.UIController+<CheckNFT>d__8.MoveNext () (at Assets/Moralis Web3 Unity SDK/Demos/Introduction/Scripts/UIController.cs:111)
--- End of stack trace from previous location where exception was thrown ---
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__6_0 (System.Object state) (at <695d1cc93cca45069c528c15c9fdd749>:0)
UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () (at /Users/bokken/buildslave/unity/build/Runtime/Export/Scripting/UnitySynchronizationContext.cs:153)
UnityEngine.UnitySynchronizationContext:ExecuteTasks() (at /Users/bokken/buildslave/unity/build/Runtime/Export/Scripting/UnitySynchronizationContext.cs:107)

Is any way to see what is the problem?

Thanks a lot

is the build platform set to webgl ? or pc ?

Hello, I found that not only the GetNFTs function, I try to call other Web3Api function and get the same error.

I tried on webgl build and the connect wallet function success, but calling the GetNFTs function or other Web3Api function will have error.

When I tried on editor, the error message is that I send out here before.

Do you have idea if I missed something? Do I need to input the X-API-Key in Unity?

Because I just try this: https://deep-index.moralis.io/nft-docs/#/NFT%20Endpoints/getNFTs
and it requires me to input the X-API-Key to proceed.

If you use the API directly (HTTP request), you will need to add a header of "X-API-Key": "your API key".

If you mean using that Swagger page to test with, then you also have to put in your API key (click Authorize in top right corner).

Thanks.
I cold use Swagger page with no problem. My previous issue is calling the web3api function in Unity.

I found that my error is coming from this line of code (in ApiClient.cs line 97):

request.Headers.Add(new KeyValuePair<string, string>(defaultHeader.Key, defaultHeader.Value));

Problem solved, use the latest version unity-web3-game-kit 1.2.7 will have no problem.
(I downloaded the web3-unity-sdk which is not a released version.)

Thanks for all your time.