Unity NullReferenceException: Object reference not set to an instance of an object

0 votes

I'm trying to access the score board and keep getting errors, even though it seems to be working. This would be fine but it is making my iOS build crash.

This is the exception:

NullReferenceException: Object reference not set to an instance of an object
com.shephertz.app42.paas.sdk.csharp.connection.RESTConnectorWWW+<Execute>d__6.MoveNext ()
com.shephertz.app42.paas.sdk.csharp.connection.RESTConnectorWWW+<WaitForRequest>d__3.MoveNext ()
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
 
This is the code:
 
using com.shephertz.app42.paas.sdk.csharp;
using com.shephertz.app42.paas.sdk.csharp.game;
using com.shephertz.app42.paas.sdk.csharp.user;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class SetupScreen : MonoBehaviour
{
    public InputField userName;
    public Text errors;
 
    private ScoreBoardService scoreBoardService;
 
    private void Start()
    {
        App42Log.SetDebug(true);
        App42API.Initialize(Strings.App42.API_KEY, Strings.App42.SECRET_KEY);
        scoreBoardService = App42API.BuildScoreBoardService();
    }
 
    public void SubmitUserName()
    {
        string name = userName.text;
        if (!string.IsNullOrEmpty(name))
            UserNameUniqueCheck(name);
        else
            errors.text = "Enter a username ya dope!";
    }
 
    private void UserNameUniqueCheck(string name)
    {
        scoreBoardService.GetLastScoreByUser(Strings.App42.GAME_NAME, name, new CheckForScoreCallBack(this));
    }
 
    internal void ScoreDoesntExist()
    {
        string name = userName.text;
        int score = PlayerPrefs.GetInt(Strings.Prefs.HIGHEST_SCORE);
        PlayerPrefs.SetString(Strings.Prefs.USERNAME, name);
 
        scoreBoardService.SaveUserScore(Strings.App42.GAME_NAME, name, score, null);
        scoreBoardService.GetLastScoreByUser(Strings.App42.GAME_NAME, name, new SaveScoreCallBack(this));
    }
 
    internal void ScoreExists()
    {
        errors.text = "Username taken ya scrub!";
    }
 
    internal void ShowSavingError()
    {
        errors.text = "Uh oh, why no internets?";
    }
 
    internal void ChangeScene()
    {
        UnityEngine.SceneManagement.SceneManager.LoadScene(Strings.Scene.TITLE);
    }
 
    private class CheckForScoreCallBack : App42CallBack
    {
        private SetupScreen sc;
 
        public CheckForScoreCallBack(SetupScreen sc)
        {
            this.sc = sc;
        }
 
        public void OnSuccess(object response)
        {
            sc.ScoreExists();
        }
 
        public void OnException(Exception e)
        {
            sc.ScoreDoesntExist();
        }
    }
 
    private class SaveScoreCallBack : App42CallBack
    {
        private SetupScreen sc;
 
        public SaveScoreCallBack(SetupScreen sc)
        {
            this.sc = sc;
        }
 
        public void OnSuccess(object response)
        {
            Game game = (Game)response;
            string userName = PlayerPrefs.GetString(Strings.Prefs.USERNAME);
 
            for (int i = 0; i < game.GetScoreList().Count; i++)
            {
                string un = game.GetScoreList()[i].GetUserName();
                if (un.Equals(userName))
                {
                    string scoreId = game.GetScoreList()[i].GetScoreId();
                    PlayerPrefs.SetString(Strings.App42.SCORE_ID, scoreId);
                    break;
                }
            }
            sc.ChangeScene();
        }
 
        public void OnException(Exception e)
        {
            Debug.Log(e.Message);
            sc.ChangeScene();
        }
    }
}
 

 

asked May 4 in Unity by Levi Stewart (10 points)

1 Answer

0 votes
Hello Stewart,

Apologies for delayed response.

I have gone through your code and you are calling APIs of App42 Leaderboard. I think you are getting this exception as you are passing callabck as null in SaveUserScore API. The API does not expect a null value. Please check and confirm the same at your end.

Thanks.
answered May 7 by rajeev.etc (1,558 points)
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com