0

I have 2 almost the same codes: It's for Likes:

private async Task HandleCallbackQuery(CallbackQuery callbackQuery, CancellationToken cancellationToken)
{
    switch (callbackQuery.Data)
    {
        // This is the case for Likes
        case "post_id like":
        {
            var postData = _context.Posts
                .FirstOrDefault(x => x.MessageId == callbackQuery.Message.MessageId);
            //var postData = conn.ExecuteReader($"SELECT * FROM PostData AS pd WHERE pd.MessageId = '{ callbackQuery.Message.MessageId }'").Parse<PostData>().FirstOrDefault();
            if (postData == null)
            {
                return;
            }
            var reader = _context.Votes
                .FirstOrDefault(x => x.VoterId == callbackQuery.From.Id && x.PostDataId == postData.Id);
            var voteData = reader;
            // ...
        }
        // This is the case for Dislikes:
        case "post_id dislike":
        {
            var postData = _context.Posts
                .FirstOrDefault(x => x.MessageId == callbackQuery.Message.MessageId);
            //var postData = conn.ExecuteReader("SELECT * FROM PostData AS pd WHERE pd.MessageId = @MessageId", new { callbackQuery.Message.MessageId }).Parse<PostData>().FirstOrDefault();

            if (postData == null)
            {
                return;
            }

            var reader = _context.Votes
                .FirstOrDefault(x => x.VoterId == callbackQuery.From.Id && x.PostDataId == postData.Id);
            //conn.ExecuteReader("SELECT * FROM VoteData AS vd WHERE vd.VoterId = @VoterId AND vd.PostDataId = @PostDataId", new { VoterId = callbackQuery.From.Id, PostDataId = postData.Id });

            var voteData = reader;
            // ...
        }
    }
}

In the first time this code runs normally, but for the second time I always in postData in dislikes have "NullReferenceException", I don't know why :/

JieBaef
  • 122
  • 1
  • 11
Taifunov
  • 533
  • 1
  • 4
  • 9
  • 3
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Panagiotis Kanavos Sep 17 '19 at 09:39
  • NREs *always* occur when you try to access a method or property on a null value. You didn't post the actual exception or mentioned where the exception occurred so one can only guess. Debug your code and check the variables in the line where the error is thrown – Panagiotis Kanavos Sep 17 '19 at 09:41
  • 1
    BTW the duplicate question explains the causes and techniques to debug an NRE in detail – Panagiotis Kanavos Sep 17 '19 at 09:42
  • http://prntscr.com/p76n4g - this is photo of locals – Taifunov Sep 17 '19 at 09:48
  • Read the duplicate question. The instructions are there. *You* can debug this far faster than anyone else. HInt: which variable is null? – Panagiotis Kanavos Sep 17 '19 at 09:53
  • postData, voteData, del - because first i take info from postData then goes with this to another variables in voteData i write PostDataId to postData.Id (this also an error) – Taifunov Sep 17 '19 at 09:56
  • The screenshot shows that the variable *reader* is null. Did you check it? – Panagiotis Kanavos Sep 17 '19 at 10:00
  • var reader = _context.Votes .FirstOrDefault(x => x.VoterId == callbackQuery.From.Id && x.PostDataId == postData.Id); This one. I already wrote why it's null - because first i take info from postData then goes with this to another variables in voteData i write PostDataId to postData.Id (this also an error) – Taifunov Sep 17 '19 at 10:02

1 Answers1

-1

The problem is _context.Posts hasn't a value...

if the _context.Posts contains null you won't get NullReferenceException


How to solve it?

Check _context.Posts,

If it's a property let it has a default value

Or if it's a field let it has null at first


If you can add more information about Posts add it...

Muaath
  • 579
  • 3
  • 19