0

I am getting this error when I tried to make a put request in my react app. This code snippet is part of my web API.

[HttpGet("{id}", Name = "Get")]
            public Users Get(int id)
            {
                var user = _context.Users.Find(id);
                UserRole userRole = _context.UserRole.FirstOrDefault(c => c.UserId == id);
                user.RoleID = userRole.RoleId;
                return user;
            }
Maryam
  • 357
  • 1
  • 5
  • 16
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Patrick Hollweck Feb 20 '19 at 14:09

1 Answers1

0
UserRole userRole = _context.UserRole.FirstOrDefault(c => c.UserId == id);
//here check if userRole is not null:
if (userRole != null)
{
    user.RoleID = userRole.RoleId;
}
jaro
  • 211
  • 2
  • 7