C#
C#

help

Root Question Message

noteason
noteason11/20/2022
❔ Application crashes

drpcclient = new DiscordRpcClient("appid was here");
drpcclient.CurrentUser.ID.ToString(); //(crashes)

https://github.com/Lachee/discord-rpc-csharp
phaseshift
phaseshift11/20/2022
What's the error?
noteason
noteason11/20/2022
let me see
noteason
noteason11/20/2022
Object referance not set to an instance of an object
phaseshift
phaseshift11/20/2022
Well there you go
phaseshift
phaseshift11/20/2022
Probably no current user
noteason
noteason11/20/2022
im the current user
noteason
noteason11/20/2022
🙃
phaseshift
phaseshift11/20/2022
Are you? So you verified current user is not null?
Anchy
Anchy11/20/2022
looks like you may need to subscribe to the ready event before accessing some objects
noteason
noteason11/20/2022
ah
noteason
noteason11/20/2022
ill try that
Anchy
Anchy11/20/2022
and also initialize the client
Anchy
Anchy11/20/2022
the github repo has some examples on how to use it
noteason
noteason11/20/2022
ye
noteason
noteason11/20/2022
drpcclient itself is nothing
noteason
noteason11/20/2022
so i cant really check for the event
noteason
noteason11/20/2022
oh wait nvm i found out
noteason
noteason11/20/2022
thank you for the help!
noteason
noteason11/20/2022
so right now my code is
Initialize();
try
{
  discorduser.Content = drpcclient.CurrentUser.Username.ToString();
}
noteason
noteason11/20/2022
and i still get
Object referance not set to an instance of an object
Kouhai
Kouhai11/20/2022
Try to subscribe to OnReady before running this code
noteason
noteason11/20/2022
ahk
Kouhai
Kouhai11/20/2022
As you can see in the source code CurrentUser is set and later OnReady is invoked
noteason
noteason11/20/2022
okay, right now this is what it looks like:
Initialize();
drpcclient.OnReady += (sender, e) =>
{
  discorduser.Content = drpcclient.CurrentUser.Username.ToString();
};
noteason
noteason11/20/2022
the on ready is not triggered
noteason
noteason11/20/2022
most likely im just really confused in some way
Kouhai
Kouhai11/20/2022
Have you tried adding a logger as well?
noteason
noteason11/20/2022
logger for?
Anchy
Anchy11/20/2022
try subscribe to the event before calling Initialize
Anchy
Anchy11/20/2022
as you may be subscribing after the client is ready
noteason
noteason11/20/2022
oke
noteason
noteason11/20/2022
then error and crash
Anchy
Anchy11/20/2022
you need to provide the error, and probably more code than you are showing
noteason
noteason11/20/2022
Object referance not set to an instance of an object
            InitializeComponent();

            try
            {
                drpcclient.OnReady += (sender, e) =>
                {
                    discorduser.Content = drpcclient.CurrentUser.Username.ToString();
                };
            }
            catch (Exception ex)
            {
              Console.Write(ex.ToString());
            }

            Initialize();
Kouhai
Kouhai11/20/2022
Initialize(); calls Initialize(); on drpcclient?
Anchy
Anchy11/20/2022
where is your rpc instantiation and Initialize looks like it is not called on your RPC
noteason
noteason11/20/2022
correct
Kouhai
Kouhai11/20/2022
And is drpcclient even initialized?
Anchy
Anchy11/20/2022
this looks like the constructor for a WPF Window
noteason
noteason11/20/2022
        public void Initialize()
        {
            drpcclient = new DiscordRpcClient("");

            drpcclient.Logger = new ConsoleLogger() { Level = LogLevel.Warning };

            drpcclient.OnReady += (sender, e) =>
            {
                Console.WriteLine("Received Ready from user {0}", e.User.Username);

            };

            drpcclient.OnPresenceUpdate += (sender, e) =>
            {
                Console.WriteLine("Received Update! {0}", e.Presence);
            };

            drpcclient.Initialize();

            drpcclient.SetPresence(new RichPresence()
            {
                Details = "In Launcher",
                Timestamps = Timestamps.Now

            });
        }
noteason
noteason11/20/2022
yup
Anchy
Anchy11/20/2022
right, you are trying to subscribe to an event before you even instantiate DiscordRpc as well
Anchy
Anchy11/20/2022
looks like you're trying to subscribe twice
noteason
noteason11/20/2022
ill try to remove the first one
noteason
noteason11/20/2022
maybe thats why
noteason
noteason11/20/2022
thank youuuu
noteason
noteason11/20/2022
it worked
noteason
noteason11/20/2022
!!!
noteason
noteason11/20/2022
now i got another issue more including the wpf part
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy