0

I am trying to return a list with all the Chat Buddies of a User. Chat Buddies meaning, all the UsersID's,where in messages I am the Sender or the Receiver. Here is my code:

public List<string> GetUserMessagesGrouped(string userId)
    {
        var a = _context.Users.FirstOrDefault(x => x.Id == userId).SentMessages.Select(m => m.ReceiverID);
        var b = _context.Users.FirstOrDefault(y => y.Id == userId).ReceivedMessages.Select(m => m.SenderID);

        var ChatBuddies = a.Concat(b).Distinct();
        return  ChatBuddies.ToList();
    }

The problem is: I am getting this Exception. System.ArgumentNullException: 'Value cannot be null.'

Although in my Database, this exact User I am testing has SentMessages and also ReceivedMessages.

enter image description here

enter image description here

Fixing points

var user = _context.Users.Include("ReceivedMessages").Include("SentMessages").FirstOrDefault(x => x.Id == userId);
        var received = user.ReceivedMessages.Select(r => r.SenderID).ToList();
        var sender = _context.Messages.Where(m => m.SenderID == userId).Select(s => s.ReceiverID).ToList();

        var ChatBuddies = received.Union(sender).ToList();

        return ChatBuddies;

The key was using .Include("ReceivedMessages").Include("SentMessages") so that they weren't null.

mjwills
  • 23,389
  • 6
  • 40
  • 63
nouko
  • 11
  • 6
  • Whenever you get an error like that, split the code into multiples lines. So instead of `var a = _context.Users.FirstOrDefault(x => x.Id == userId).SentMessages.Select(m => m.ReceiverID);` Do `var a = _context.Users; var b = a.FirstOrDefault(x => x.Id == userId);` etc etc You will spot the issue much more quickly that way. – mjwills May 21 '18 at 21:55
  • 2
    OP, please post the exception's stack trace, and indicate in your code example which line of code causes the exception. – John Wu May 21 '18 at 22:51
  • Do all of your sent messages have a `ReceiverID`? And do all of your received messages have a `SenderID`? – John Wu May 21 '18 at 22:55
  • Your `Fixing points` should be added as an answer rather than updating the question. That way everyone can benefit from your findings. _I have also edited your question down to just the relevant parts, to make it easier for others to learn from it in future._ – mjwills May 23 '18 at 00:57

2 Answers2

1

You need to check the documentation for FirstOrDefault() - in particular check what is returned when the default value is returned.

You may want to use the Elvis operator ?. as a way of avoiding your null reference exception.

slugster
  • 49,403
  • 14
  • 95
  • 145
-1

Place a breakpoint where the method is called and check the value of userId. It says null arguments which can be caused by a null argument given to the method.

Additionally you can just create a test var like this:

var userIdTest = userId; 

And place a breakpoint to check if it actually gets inside the method as defined, also setting a default value:

public List<string> GetUserMessagesGrouped(string userId = ""){}

Should prevent this Exception. You can also use a try catch block to stack trace it to the source.

try{ method body }
catch(ArgumentNullException exception){
   Console.WriteLine(ex); 
}

You can just place a breakpoint in the catch keyword alternatively to Console Logging it. I hope I could be of help and good luck!

Rafael Rocha
  • 508
  • 1
  • 3
  • 16
  • Step into the query thats causing the exception to be thrown and you might be able to follow it to the source of the problem. At this stage there's really not much that I can tell you since to replicate your problem I would have to be in the same context. I'm sorry and I hope you can figure it out. – Rafael Rocha May 22 '18 at 08:20