[Help] Spammed in chat with object reference not set to an instance of an object

Hi Guys

I have used HyperFive as a resource to make my server and client to share data.

I have run into an issue trying to use the GetProtectedData on a UI system to display the players cash. The error I am getting is

|Error|CS1503|Argument 2: cannot convert from 'System.Collections.Generic.Dictionary<string, dynamic>' to 'EGRP.Shared.Data.SessionDataModel'

The line of the error is the ‘data.ProtectedData’ in the Money class for the UI system:


var data = new SessionDataModel();

                // Setting Cash UI Text // NEED TO FIND THE MODEL INFO FOR THE SESSION!!!!
                var session = new Session(Player.ServerId, data.ProtectedData);

                string cashText = $"${session.GetProtectedData("Cash.Bank")}";

                DrawText($"{cashText}", 1617f, 43f, 0.7f, 255, 255, 255, 255, 0, 0, false, false, 0, 0, 0);

The Session code:

public Session(int netId, SessionDataModel model)
        {
            NetId = netId;

            Name = model.Name;

            foreach (var kvp in model.ProtectedData)
            {
                _protectedData[kvp.Key] = kvp.Value;
            }

            foreach (var kvp in model.SharedData)
            {
                _sharedData[kvp.Key] = kvp.Value;
            }
        }

And finally just incase you need the SessionDataModel:

public class SessionDataModel
    {
        public int NetId { get; set; }
        public string Name { get; set; }
        public Dictionary<string, dynamic> SharedData { get; set; }
        public Dictionary<string, dynamic> ProtectedData { get; set; }

    }

This has kinda been taken straight from HyperFive by MoosheTV. He gave examples on how to set the data but not get it unfortuantely.

Thanks in Advance.

Pass data, not data.ProtectedData.

The parameter is expecting a SessionDataModel object, but you are passing a Dictionary<string, dynamic> object.

I have been sitting for half the day staring at all the code trying to figure it out lol. Seriosuly cant believe I didnt try that even haha. Thanks so much :smiley:

Now seem to be getting spammed in chat with object reference not set to an instance of an object.

private async Task OnTick()
        {
            try
            {


                // Setting the Text Color
                var color = System.Drawing.Color.FromArgb(255, 255, 255, 255);
                // Setting the Text Position
                Vector2 position = new Vector2(0.18f, 0.93f);

                var data = new SessionDataModel();

                // Setting Cash UI Text // NEED TO FIND THE MODEL INFO FOR THE SESSION!!!!
                var session = new Session(Player.ServerId, data);

                string cashText = $"${session.GetProtectedData("Cash.Bank")}";

                DrawText($"{cashText}", 1617f, 43f, 0.7f, 255, 255, 255, 255, 0, 0, false, false, 0, 0, 0);
            }
            catch (Exception ex)
            {
                Log.Info(ex.Message);
            }
            await Task.FromResult(0); 
        }

How would I Instantiate the var data and var session?

So I Instantiated the vars in the Money class, but now get thrown a:

[   2360953] Failed to instantiate instance of script EGRP.Client.Classes.Environment.UI.Money: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.

[   2360968]   at EGRP.Client.Classes.Managers.Session..ctor (System.Int32 netId, EGRP.Shared.Data.SessionDataModel model) [0x0003e] in E:\Git\FiveM\EGRP\EGRP\Classes\Managers\Session.cs:35 

[   2360968]   at EGRP.Client.Classes.Environment.UI.Money..ctor () [0x00026] in E:\Git\FiveM\EGRP\EGRP\Classes\Environment\UI\Money.cs:37 

[   2360968]   at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (System.Reflection.MonoCMethod,object,object[],System.Exception&)

[   2360968]   at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00002] in <f0e9de1592254c6a8c7c298c474f20de>:0 

[   2360968]    --- End of inner exception stack trace ---

[   2360968]   at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00014] in <f0e9de1592254c6a8c7c298c474f20de>:0 

[   2360968]   at System.RuntimeType.CreateInstanceMono (System.Boolean nonPublic) [0x000a8] in <f0e9de1592254c6a8c7c298c474f20de>:0 

[   2360968]   at System.RuntimeType.CreateInstanceSlow (System.Boolean publicOnly, System.Boolean skipCheckThis, System.Boolean fillCache, System.Threading.StackCrawlMark& stackMark) [0x00009] in <f0e9de1592254c6a8c7c298c474f20de>:0 

[   2360968]   at System.RuntimeType.CreateInstanceDefaultCtor (System.Boolean publicOnly, System.Boolean skipCheckThis, System.Boolean fillCache, System.Threading.StackCrawlMark& stackMark) [0x00027] in <f0e9de1592254c6a8c7c298c474f20de>:0 

[   2360968]   at System.Activator.CreateInstance (System.Type type, System.Boolean nonPublic) [0x00020] in <f0e9de1592254c6a8c7c298c474f20de>:0 

[   2360968]   at System.Activator.CreateInstance (System.Type type) [0x00000] in <f0e9de1592254c6a8c7c298c474f20de>:0 

[   2360968]   at CitizenFX.Core.InternalManager.CreateAssemblyInternal (System.String assemblyFile, System.Byte[] assemblyData, System.Byte[] symbolData) [0x000bb] in C:\gl\builds\edf06b9b\0\cfx\fivem\code\client\clrcore\InternalManager.cs:103 
[   2360968] Instantiated instance of script EGRP.Client.Classes.Environment.UI.UI.

I dont understand if I have done something wrong on the UI side or if its the Session side, If its the session side Im not sure how to instantiate it there because of the foreach.

Here is the Session.cs, The error is on the first foreach for ProtectedData

private readonly Dictionary<string, dynamic> _protectedData = new Dictionary<string, dynamic>();
        private readonly Dictionary<string, dynamic> _sharedData = new Dictionary<string, dynamic>();

        /// <summary>
        /// The time the session was created in UTC. Not accurate to the exact time they connected as this is dependent
        /// on the time it takes for them to load in.
        /// </summary>
        public readonly DateTime JoinTime = DateTime.UtcNow;

        public Session(int netId, SessionDataModel model)
        {
            NetId = netId;

            Name = model.Name;

            foreach (var kvp in model.ProtectedData)
            {
                _protectedData[kvp.Key] = kvp.Value;
            }

            foreach (var kvp in model.SharedData)
            {
                _sharedData[kvp.Key] = kvp.Value;
            }
        }

My Money UI.cs

public Money()
        {
            Tick += OnTick;

            //Loading the SessionDataModel from Shared
            data = new SessionDataModel();

            //Loading the Session for Client
            session = new Session(Player.ServerId, data);
        }

        private async Task OnTick()
        {
            //if (CinematicMode.DoHideHud) return;
            try
            {
                // Setting the Text Color
                var color = System.Drawing.Color.FromArgb(255, 255, 255, 255);
                // Setting the Text Position
                Vector2 position = new Vector2(0.18f, 0.93f);

                /*
                 * 
                 * Need to Instantiate both var data and var session for the UI to work...
                 * 
                 */

                // Setting Cash UI Text
                string cashText = $"${session.GetProtectedData("Bank.Cash")}";

                DrawText($"{cashText}", 1617f, 43f, 0.7f, 255, 255, 255, 255, 0, 0, false, false, 0, 0, 0);
                Log.Info(cashText);
            }
            catch (Exception ex)
            {
                Log.Info(ex.Message);
            }
            await Task.FromResult(0); 
        }

Where does Player.ServerId come from? Shouldn’t it be Game.Player.ServerId? I would also instantiate session on first tick instead of in the constructor. Game.Player.ServerId might not exist yet when the class gets instantiated, so do it when you know the game is running, aka first tick.

Alright so I changed it to Game.Player.ServerId and I put it under OnTick with what I think would be first tick? But its still giving me error:

[    558140] [INFO] Object reference not set to an instance of an object.
[    558156] [INFO] Object reference not set to an instance of an object.
[    558172] [INFO] Object reference not set to an instance of an object.

The code now looks like this:

 private async Task OnTick()
        {
            try
            {
                if (firstTick == true)
                {
                    //Loading the Session for Client
                    session = new Session(Game.Player.ServerId, data);

                    firstTick = false;
                }

                // Setting the Text Color
                var color = System.Drawing.Color.FromArgb(255, 255, 255, 255);
                // Setting the Text Position
                Vector2 position = new Vector2(0.18f, 0.93f);

                // Setting Cash UI Text
                string cashText = $"${session.GetProtectedData("Bank.Cash")}";

                DrawText($"{cashText}", 1617f, 43f, 0.7f, 255, 255, 255, 255, 0, 0, false, false, 0, 0, 0);
                Log.Info(cashText);
            }
            catch (Exception ex)
            {
                Log.Info(ex.Message);
            }
            await Task.FromResult(0); 
        }

Not sure if its just cause im really tired or if something is wrong somewhere where we not looking.

Probably something with session or data then. Try to catch things more often to see exactly which object is null