I'm searching to get some players infos from a database with events in C#.
When I try my script in game, this error message appears :
[ 9419687] [b2699_GTAProce] MainThrd/ System.NullReferenceException: Object reference not set to an instance of an object.
[ 9419687] [b2699_GTAProce] MainThrd/
[ 9419687] [b2699_GTAProce] MainThrd/
[ 9419687] [b2699_GTAProce] MainThrd/ Server stack trace:
[ 9419687] [b2699_GTAProce] MainThrd/ at CitizenFX.Core.InternalManager.PrintError (System.String where, System.Exception what) [0x00081] in C:\gl\builds\master\fivem\code\client\clrcore\InternalManager.cs:595
[ 9419687] [b2699_GTAProce] MainThrd/ at CitizenFX.Core.InternalManager.CallRef (System.Int32 refIndex, System.Byte[] argsSerialized, System.IntPtr& retvalSerialized, System.Int32& retvalSize) [0x000b6] in C:\gl\builds\master\fivem\code\client\clrcore\InternalManager.cs:527
[ 9419687] [b2699_GTAProce] MainThrd/ at (wrapper remoting-invoke-with-check) CitizenFX.Core.InternalManager:CallRef (int,byte[],intptr&,int&)
[ 9419687] [b2699_GTAProce] MainThrd/ at (wrapper xdomain-dispatch) CitizenFX.Core.InternalManager:CallRef (object,byte[]&,byte[]&,int,byte[],int&)
[ 9419687] [b2699_GTAProce] MainThrd/
[ 9419687] [b2699_GTAProce] MainThrd/ Exception rethrown at [0]:
[ 9419687] [b2699_GTAProce] MainThrd/ at (wrapper xdomain-invoke) CitizenFX.Core.InternalManager:CallRef (int,byte[],intptr&,int&)
[ 9419687] [b2699_GTAProce] MainThrd/ at (wrapper remoting-invoke-with-check) CitizenFX.Core.InternalManager:CallRef (int,byte[],intptr&,int&)
[ 9419687] [b2699_GTAProce] MainThrd/ at CitizenFX.Core.MonoScriptRuntime.CallRef (System.Int32 refIndex, System.Byte[] argsSerialized, System.Int32 argsSize, System.IntPtr& retvalSerialized, System.Int32& retvalSize) [0x00013] in C:\gl\builds\master\fivem\code\client\clrcore\MonoScriptRuntime.cs:228
If you want to see it, here is my two scripts, the first is in the client side, the second one is in the server side.
Client-side :
public void GetPlayerInformations()
{
TriggerServerEvent("MST_SV_GetInformationOfPlayer",new Action <string, int, string>((userId, department, callsign) =>
{
_Functions.SendTextToConsole(Functions.InfoType.Debug, $"UserId: {userId} | Department: {department} | Callsign: {callsign}");
if (userId != null)
{
UserId = userId;
Department = department;
Callsign = callsign;
}
}));
}
Server-side :
private static void GetInformationOfPlayer([FromSource] Player player, NetworkCallbackDelegate callback)
{
try
{
using (MySqlConnection connection = Database.GetConnection())
{
using (MySqlCommand command = connection.GetCommand())
{
command.CommandText = "select * from user where license = @identifier";
command.Parameters.AddWithValue("@identifier", player.Identifiers["license"]);
using (DbDataReader result = command.ExecuteReader())
{
if (result.Read())
{
_functions.SendTextToConsole(Functions.InfoType.Debug,$"License : {result["license"]}, Department : {result["department"]}, Callsign : {result["callsign"]}.");
callback.Invoke(Convert.ToString(result["license"]), Convert.ToInt32(result["department"]), Convert.ToString(result["callsign"]));
_functions.SendTextToConsole(Functions.InfoType.Info, $"Informations of player {player.Name} has been loaded successfully.");
result.Close();
}
else
{
// Other part of the script non related to my issue
}
}
}
}
} catch (Exception e)
{
_functions.SendTextToConsole(Functions.InfoType.Error, $"An error has occurred while loading informations of player {player.Name} : {e.Message}");
}
}
I already check the database connection (it is okay), differents variables types, and the three variables gave through the callback aren't null (checked with the Debug.WriteLine) ...