简体   繁体   中英

Function Returning a null Entity Error

I been stuck on this problem for a few days and am getting pretty frustrated so maybe someone can see it better than I can. This is an application written in C# and using an Entity framework. A bit of a background of what this code is suppose to do is:

The application is for oil trucking industry where they will weekly/daily production reported and saved in the database. The reports can be based on either Units or Wells and other things.

Anyway below is the code where I believe is the problem.

/// <summary>
/// Returns a Production entity if one exists or null if it doesnt exist
/// </summary>
/// <returns></returns>

        private Production GetProduction(DateTime dt)
      {
        Production existingProduction;
        var qry = _db.Productions.AsNoTracking().Where(x => x.ProductionDate == dt);
        if (IsUnitSelected)
            existingProduction = qry.Where(x => x.UnitID == SelectedUnit.Key && x.Well                 == null).FirstOrDefault();
        else
            existingProduction = qry.Where(x => x.WellID == SelectedWell.Key && x.Unit   == null).FirstOrDefault();

        return existingProduction;
       }

Basically this code is suppose to find the production entity that corresponds to selected day of the week that was selected and return it then add it to a Production variable called existingProduction but if it doesnt find one it returns null.

The code below is the declarations of the values used where GetProduction is parameters are set:

      var containers = _db.SourceContainers.ToList();

        //Gets the currently selected Day
        var view = (ProductionEntryView)this.GetView();
        var date = view.SelectedWeekDay.Key;

        foreach (var productionItem in Data.Productions)
        {
            //If Weekly Mode is on && Only validate data for the current day selected
            if (!saveAll && !productionItem.Key.Equals(date))
                    continue;


            Production existingProduction = GetProduction(productionItem.Key);


            existingProduction.ProductionDate = productionItem.Key;
            existingProduction.IsRunning = productionItem.Value.IsRunning.Value;
            existingProduction.Remarks = productionItem.Value.Remarks;

So after all this i keep get errors because GetProduction keeps returning null and adding it to existingProduction. So if anyone can possibly help me out or even guiding me on a way to better find the solution that would be great too. Im pretty new to Entity and C# so any advice would be awesome. Thank You

I'd absolutely wrap the usage of existingProduction with a null check before trying to do anything with existingProduction :

Production existingProduction = GetProduction(productionItem.Key);
if (existingProduction != null)
{
    existingProduction.ProductionDate = productionItem.Key;
    existingProduction.IsRunning = productionItem.Value.IsRunning.Value;
    existingProduction.Remarks = productionItem.Value.Remarks;
    // any other code that tries to use the existingProduction variable here
}

Of course, if you are getting back nulls from GetProduction() when you think you should be getting non-null values, that's a different problem. The above code only avoids null reference exceptions when existingProduction ends up as null .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM