1

This is something that worked up to now and now it just, stopped working (I know, weird, there's probably some silly mistake..)

I have a TripsVM, which contains a list of trips. I load these in my service, returning a List<>.

The problem occurs when I iterate over the trips collection and try to get trip.TripCategory.Name, as the TripCategory is empty, even though TripCategoryID has a value.

This all happens at the backend, I load the trips and then try to iterate over them, they are not being send from the page.

I could probably just load the trip by trip itself, but it used to work and this bug just came up after months of usage.

Any suggestions of where to look for bugs would be really appreciated.

Thanks

Error:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Where error occurs:

foreach (Trip trip in tripsVM.TripsList) {
    var a = trip.TripCategory.Name;

TripsVM:

    private List<Trip> _TripsList;

    public List<Trip> TripsList
    {
        get
        {
            if (_TripsList == null)
            {
                _TripsList = TripsService.GetTrips();

                if (_TripsList == null)
                    _TripsList = new List<Trip>();
            }

            return _TripsList;
        }
        set { _TripsList = value; }
    }

Service:

public static List<Trip> GetTrips()
{ 
     return DB.Trips.Where(...).OrderBy(...).ToList(); 
}

Trip class:

public partial class Trip
{
    public int TripID { get; set; }
    public int TripCategoryID { get; set; }
    ....
    public virtual TripCategory TripCategory { get; set; }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mara
  • 118
  • 1
  • 10
  • 1
    have you even read my Q ??? ofc not, completely different topic, thanks for ruining my Q by marking it as a duplicate, READ first – Mara Jan 05 '15 at 17:30
  • Well I'm sorry you feel that way but the linked answer WILL help you. – DavidG Jan 05 '15 at 18:04
  • 1
    If you have read it, you would know that it wont help me. I went through that link and its great collection of how to avoid null ref errors, but as I said, in my case null ref error is a result of some other bug and your post is not helpful for this case. – Mara Jan 06 '15 at 11:26

2 Answers2

2

Its looks like your DB context disposed before foreach code or LazyLoadingEnabled set to false in context.

In Service add using

using System.Data.Entity;

And modify loading method

public static List<Trip> GetTrips()
{ return DB.Trips.Where(...).Include(t=>t.TripCategory).OrderBy(...).ToList(); }
YuriyP
  • 4,210
  • 4
  • 25
  • 35
  • Wow, just adding the using System.Data.Entity helped!! Thank you so much, I have spent around 4h dealing with this.. Can you point me in a direction of some research about this topic? I dont really understand why did it work for some time and now I had to add this using and that the compiler didnt mind... Thanks ! – Mara Jan 06 '15 at 11:28
  • I highly doubt that only adding using fix problem. I guess that some of your code set LazyLoadingEnabled=false on context. And if it executed before GetTrips() than Object reference appears, otherwise GetTrips() works fine. – YuriyP Jan 08 '15 at 17:22
  • Anyway I consider to add .Include(t=>t.TripCategory) in your code for perfomance reason if you always need .TripCategory property information. – YuriyP Jan 08 '15 at 17:29
0

I think your code looks fine but you should add some if statements to avoid null exception, because you are returning something with where clause, so you might end up with empty query result and empty list, and in that list you are trying to reach an element of a list object:

if(tripsVM.TripsList != null){
     foreach (Trip trip in tripsVM.TripsList) {
     var a = trip.TripCategory.Name;
     }
}

else
{
   // handle empty list
}


 private List<Trip> _TripsList;

        public List<Trip> TripsList
        {
            get
            {
            _TripsList = new List<Trip>();
            if(TripsService.GetTrips() != null)
              {
             _TripsList.add(TripsService.GetTrips());
              }
                return _TripsList;
            }
            set { _TripsList = value; }
        }
knightRider
  • 153
  • 9
  • Hi, thanks for the answer, but IMO its ok in this case (I deal with it inside the property). The problem here was that the list was populated, but navigation properties of the Trip were empty. Resolved bellow :) – Mara Jan 06 '15 at 11:31