1

My code:

ret.chromeVehicles = _context.Database.SqlQuery<ChromeVehicle>("SELECT Year, Make, Model, ModelNumber, MIN(VIN) as VIN FROM DealerInventory WHERE DealerID = 1 GROUP BY Year, Make, Model, ModelNumber").ToList();
List<Thread> threadList = new List<Thread>();
foreach (ChromeVehicle vehicle in ret.chromeVehicles)
{
    var thread = new Thread(delegate () {

        // Here is an API call which fills the data variable (this works fine)

        foreach (JObject incentive in data["incentives"])
        {
            // Add finance rate incentives
            if ((string)incentive["categoryDescription"] == "Finance Rate")
            {
                // Add Chrome Incentive to Finance Rate
                var inc = new ChromeIncentive();
                inc.Group = (string)incentive["groupAffiliation"];
                inc.Ownership = (string)incentive["previousOwnership"];
                inc.ID = (int)incentive["incentiveID"];
                inc.Category = (string)incentive["categoryDescription"];

                var rate = new ChromeRate();
                rate.Incentive = inc;

                vehicle.FinanceRates.Add(rate); // Error is on this line
            }
        }
    });
    thread.Start();
}

The error I get:

Additional information: Object reference not set to an instance of an object.

The line I get the error for is vehicle.FinanceRates.Add(rate);

I've looked up this error and it says it has to do with creating an empty variable. So vehicle I assume would have to be the empty variable, but I'm not sure how it would be empty considering there are no empty rows in the db and it's coming straight from the db. If I comment out vehicle.FinanceRates.Add(rate); then it works fine pulling all of the vehicles with their data.

Any idea what's wrong in my code that could be causing this error? Perhaps the thread prevents it from editing the variable which is outside of the thread?

What the code should do:

Pull a list of unique vehicles from the db. Loop those vehicles and run an API call for each one in a different thread. Take the API data and add it to the vehicle data.

I've even tried checking if null and still I get the error:

if (vehicle != null && rate != null)
{
    vehicle.FinanceRates.Add(rate);
}

I'm thinking it has to do with Threads but what about Threads would cause that error?

Tyler
  • 3,713
  • 6
  • 37
  • 63
  • 2
    This is not a duplicate and specifically refers to a potential threading issue - the OP is not asking what a `NullRef` is, but asking how there could be one given the context of the executing code. – Charleh Oct 19 '16 at 11:12
  • 2
    Are you absolutely sure that all your `vehicle.FinanceRates` objects are actually `List`s and aren't null in any of the records? – Charleh Oct 19 '16 at 11:14
  • Oh dude... yeah, you know what, I set it as being List but now that you mention it, I didn't create a new List anywhere for it... For some reason I had it in my head that the .Add was going to create it. I added FinanceRates = new List(); to the constructor of the ChromeVehicle class and now I no longer get the error. Thanks man for not brushing this question off. Working with a new thing like threads I just assumed it had to do with that. – Tyler Oct 19 '16 at 11:19
  • 2
    No problem, hate it when questions get closed immediately, people should at least have a look at the issue before it's deemed a dupe. It turns out it was a simple problem after all, but I think the courtesy of an answer or at least a comment is due, especially since you have over 2K rep which means you are contributing to the SO community - we owe it to each other to support each other! – Charleh Oct 19 '16 at 11:23
  • 2
    I agree. Thankfully it was something simple and could be considered a duplicate, but the poster doesn't always know that, so again, thanks for taking the time to read through it. – Tyler Oct 19 '16 at 11:40

0 Answers0