sirkris / Reddit.NET

A Reddit API library for .NET Standard with OAuth support. Written in C#.
MIT License
500 stars 77 forks source link

Exception thrown: 'System.ArgumentNullException' in System.Private.CoreLib.dll #138

Open petekay opened 3 years ago

petekay commented 3 years ago

Hi, I installed the Package today, and I am trying to stream some comments/posts for two subreddits. I'm getting the following exception all the time in the console: Exception thrown: 'System.ArgumentNullException' in System.Private.CoreLib.dll

this is the code to replicate it:


        public RedditCore()
        {

            _reddit_client = new RedditClient(
                appId: "xxx",
                appSecret: "xxx",
                refreshToken: "xxx",
                userAgent: "xxx");
            System.Diagnostics.Debug.Print("RedditCore init");

        }

        public void add_subbreddit(string str_subreddit)
            if ((str_subreddit != null) & (str_subreddit.Length > 0))
            {
                var _subreddit = _reddit_client.Subreddit(name: str_subreddit);
                Debug.Print(str_subreddit);

                _subreddit.Posts.GetNew();
                _subreddit.Posts.NewUpdated += C_NewPostsUpdated;
                _subreddit.Posts.MonitorNew();

                _subreddit.Comments.GetNew();
                _subreddit.Comments.NewUpdated += C_NewCommentsUpdated;
                _subreddit.Comments.MonitorNew();
            }

        public static void C_NewCommentsUpdated(object sender, CommentsUpdateEventArgs e)
        {
            foreach (var comment in e.Added)
            {
                Debug.WriteLine("New Comment by " + comment.Author + ": " + comment.Body);
            }
        }
        public static void C_NewPostsUpdated(object sender, PostsUpdateEventArgs e)
        {
            foreach (var post in e.Added)
            {
                Debug.WriteLine("New Post by " + post.Author + ": " + post.Title);
            }
        }

from outside I'm calling the subclass:

            _reddit = new RedditCore();
            _reddit.add_subbreddit("Nike");
            _reddit.add_subbreddit("csharp");
            _reddit.add_subbreddit("dotnet");
            _reddit.add_subbreddit("news");

after some time, I get the async. messages for the Posts or Comments, but it look like this: (like 1-2 exceptions every second).

Exception thrown: 'System.ArgumentNullException' in System.Private.CoreLib.dll
Exception thrown: 'System.ArgumentNullException' in System.Private.CoreLib.dll
Exception thrown: 'System.ArgumentNullException' in System.Private.CoreLib.dll
New Comment by ... : ...
New Comment by ... : ....
https://www.nola.com/news/crime_police/article_170de372-8810-11eb-8b42-af2505d8e26a.html
Exception thrown: 'System.ArgumentNullException' in System.Private.CoreLib.dll
Exception thrown: 'System.ArgumentNullException' in System.Private.CoreLib.dll
Exception thrown: 'System.ArgumentNullException' in System.Private.CoreLib.dll
...
New Comment by ... : ...
New Comment by ... : ...
Exception thrown: 'System.ArgumentNullException' in System.Private.CoreLib.dll
Exception thrown: 'System.ArgumentNullException' in System.Private.CoreLib.dll
Exception thrown: 'System.ArgumentNullException' in System.Private.CoreLib.dll
Exception thrown: 'System.ArgumentNullException' in System.Private.CoreLib.dll
Exception thrown: 'System.ArgumentNullException' in System.Private.CoreLib.dll
Exception thrown: 'System.ArgumentNullException' in System.Private.CoreLib.dll
The thread 0x2e54 has exited with code 0 (0x0).
Exception thrown: 'System.ArgumentNullException' in System.Private.CoreLib.dll
Exception thrown: 'System.ArgumentNullException' in System.Private.CoreLib.dll
...
sirkris commented 2 years ago

Sorry for the delay. I stepped away for a few months. Based on your output, my guess is the problem is with NewPostsUpdated, since that's the one not appearing (it would help if you would test them individually so we can confirm this). ArgumentNullException suggests that one of the two args is null when it shouldn't be.

Could you run a debug and tell me what args are getting passed in the event?

Here's where the method invocation actually occurs in SubredditPosts.cs:

internal virtual void OnNewUpdated(PostsUpdateEventArgs e)
{
    NewUpdated?.Invoke(this, e);
}

I doubt the problem is with our sender (we're just passing this and it's definitely not null). So let's examine where e is coming from. In SubredditPosts.cs:

// Event handler to alert the calling app that the list has changed.  --Kris
PostsUpdateEventArgs args = new PostsUpdateEventArgs
{
    NewPosts = newList,
    OldPosts = oldList,
    Added = added,
    Removed = removed
};
TriggerUpdate(args, type);

TriggerUpdate basically just does this:

OnNewUpdated(args);

So as you can see, we're passing args and it's also definitely not null. I haven't been able to reproduce this issue, either, and all the tests are still passing. Your code also looks fine. This leads me to believe that the issue might be environment-related, though I'm not exactly sure how at the moment. Neither of those values should be null but we have that exception which says otherwise.

It's also possible that this is a red herring and the ArgumentNullException is being triggered by something else. I can't determine that without a full debug, which I'll need you to perform since I can't repro the issue on my end.

TL;DR: Please try running this in debug mode so we can get more useful information, like a stack trace on that exception.