locked
A single User gets a null reference exception when trying to save objects to the database. Other users don't RRS feed

  • Question

  • User-216792685 posted

    Hi I recently started developping with ASP.NET MVC and I just got out of school.

    In an application I am working on remotely for a company I have a very weird issue that I am not really able to understand.


    After deploying some changes on the QA environment, one user started having problems when trying to save objects to the database. The user gets a system.nullreferenceexception. This seem to be an isolated case, as I am able to save myself, I have asked some of the user's colleagues to also test the application to check if they have the same issue, but the application works perfectly for them.

    Here is one of the functions where this type of exception seems to be thrown.

     public string SaveVendor(Vendor vendor, AppUser appUser, DbConnection dbConnection = null, SqlTransaction sqlTxn = null)
            {
    
                if (vendor.Id == 0)
                    vendor.WhenCreated = DateTime.Now;
    
                vendor.WhenModified = DateTime.Now;
    
                try
                {
                    using (var context = dbConnection == null ? new ContextRepository() : new ContextRepository(dbConnection))
                    {
                        var userId = context.SecurityUsers.Where(u => u.UserName == appUser.Fullname).FirstOrDefault().SecurityUserId;
                        vendor.ModifiedBy = context.SecurityUsers.Where(u => u.UserName == appUser.Fullname).FirstOrDefault().SecurityUserId;
    
                        if (vendor.Id == 0)
                        {
                            context.Vendors.Add(vendor);
                        }
                        else
                        {
                            context.Vendors.Attach(vendor);
                            ((IObjectContextAdapter)context).ObjectContext.ObjectStateManager.ChangeObjectState(vendor, System.Data.Entity.EntityState.Modified);
                        }
    
                        context.SaveChanges(appUser.Name);
                    }
                    return "OK";
                }
                catch (Exception ex)
                {
                    _logger.Error(ex.ToString());
                    throw;
                }
            }

    All the save functions in the application follow a similar pattern, and it completely  eludes me how it is possible that one specific user cannot save anything.

    The things I have checked with the user so far as I do think it has something to do with the user computer.

    1. Force Refresh and Cookie/history clearing of the browser
    2. Verified Chrome version (Both our chrome versions are up to date (and the same))
    3. Made the user change his chrome settings to be identical to mine

    The things I still need to do

    1. Have the user try to use another person's computer to verify if it is really the computer that is the issue.
    2. Remote access the computer so I can go and get the Chrome Debugger data (Javascript errors)

    I am lost on what to do next on this case. Has anyone here ever had a similar issue? Do you have suggestions on what to try next?

    Wednesday, May 10, 2017 4:48 PM

Answers

  • User753101303 posted

    Hi,

    So it means you are trying to use an object member while the object is null.

    In your Linq queries do you expect to retrieve exactly one object? If yes I would use Single rather than FirstOrDefault (this way you'll have a particular exception if it happens). For example could it be that for this use FullName and UserName are actually different values? I see also that you have appUser.Name later.

    If not enough you could also add a check on vendor and appUser (in case it would come from some other request or whatever that might return null). But once again as it seems to happen just for this particular user it would point rather to an issue regarding data belonging to this particular user (ie some kind of UserName, FullName, Name discrepancy or confusion).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 10, 2017 5:16 PM

All replies

  • User753101303 posted

    Hi,

    So it means you are trying to use an object member while the object is null.

    In your Linq queries do you expect to retrieve exactly one object? If yes I would use Single rather than FirstOrDefault (this way you'll have a particular exception if it happens). For example could it be that for this use FullName and UserName are actually different values? I see also that you have appUser.Name later.

    If not enough you could also add a check on vendor and appUser (in case it would come from some other request or whatever that might return null). But once again as it seems to happen just for this particular user it would point rather to an issue regarding data belonging to this particular user (ie some kind of UserName, FullName, Name discrepancy or confusion).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 10, 2017 5:16 PM
  • User-216792685 posted

    I do expect only one result. The application will only have 4 to 5 users at a time. I will explore a bit further with your suggestions in mind then I will come back , thanks.

    Wednesday, May 10, 2017 5:25 PM
  • User-271186128 posted

    Hi MartinSavard81,

    According to your description, I think this exception might on your code.

    The following code will most likely to occur the nullreferenceexception.

    var userId = context.SecurityUsers.Where(u => u.UserName == appUser.Fullname).FirstOrDefault().SecurityUserId;
    vendor.ModifiedBy = context.SecurityUsers.Where(u => u.UserName == appUser.Fullname).FirstOrDefault().SecurityUserId;
    

    You should check does it has a not null value after FirstOrDefault() ?

    Or the arguments(vendor or appUser) is null?

    If you have any other questions, please feel free to contact me any time.

    Best Regards,
    Dillion

    Thursday, May 11, 2017 6:26 AM
  • User-216792685 posted

    Thank you Patrice you gave me the Hint that I needed!

    The user is authenticated elsewhere than the application itself. But authorized users also have a table in the database of the application where their login , Full Name, and access lvl is saved.
    Thing is, is that the linq queries were making a check between those two. And I found out there was a mistake in the full name inside the access rights table for that specific user. I was able to duplicate the errors by introducing an misspell in my name. I fixed my name and that person's name again, and now it looks like it works for that person.

    All the save functions in the application work the same way. And this was code that was present before I started working on the project, but I will gladly take note of that particular issue and report it when appropriate.

    Thursday, May 11, 2017 12:28 PM