0

Anytime I use combinedResultList in a query I get a null reference exception when I try to initialize it. If I don't use it in the query below it, it works fine. Is there something wrong with my query that is causing the null reference? I am just trying to get back any rows that have a coupon id contained within the combinedResultList

var combinedResultList = visionLines.Where(line => line != null).SelectMany(line => line.VisionResults).Distinct().ToList(); //Get Null reference exception here

eventsFromDb = (from e in db.Events //If I comment out this query the above statement works and no longer throws the null reference exception
                where (combinedResultList.Select(result => result.CouponId)).Contains(e.CouponIdentifier)
                && e.ExpiryDate >= _minExpiryDate
                select new EventDataTransferObject { EventIdentifier = e.EventIdentifier, DateLastUpdated = e.DateLastUpdated }).ToList();  

Error Message:

Object reference not set to an instance of an object.

Stack Trace:

at MyProject.UI.ViewModel.SelectionEditorViewModel.GetEventsByCouponName(String couponName, CancellationToken ct) in C:\MyProject.UI\ViewModel\SelectionEditorViewModel.cs:line 845

Image from Visual Studio of problem: enter image description here

Spitfire5793
  • 391
  • 2
  • 15
  • It will help to include the exact error message and stack trace as well. – ye-olde-dev May 04 '20 at 13:51
  • They arent much help but I've added them to the question – Spitfire5793 May 04 '20 at 13:57
  • IMHO what you are saying is wrong and not possible. The second query can't influence the first query to throw an NRE or not. Did you actually debug your code and moved step by step and mouse over everything to look at its value? – Rand Random May 04 '20 at 14:04
  • @RandRandom I've added a screenshot from Visual studio which perhaps illustrates the problem better. I've never seen this behaviour before either – Spitfire5793 May 04 '20 at 14:10
  • Please temporarily disable the evalution of variables from resharper and have a look again. Extensions -> Resharper -> Options -> Tools -> Debugger -> Editor Integration | Disable "Show local variables value" | "Show current line expressions values" | "Show function return values" – Rand Random May 04 '20 at 14:14
  • Please check if you have "combinedResultList" defined multiple times, as a local variable and maybe in an outer scope eg. static member, because in the current line position where you just have `var combinedResultList = new List();` there can't be a NRE, where is it getting that NRE when you havent even declared the variable jet, only on the next line the variable is declared and could be accessed. – Rand Random May 04 '20 at 14:18
  • @RandRandom Tried what you suggested, still getting the same issue. Its whenever I step onto the next line I'm getting the NRE error, it goes into my catch statement. The variable isn't declared anywhere else – Spitfire5793 May 04 '20 at 14:22
  • Enable "Require source files to exactly match the orginal version" in Tools -> Options -> Debugging -> General – Rand Random May 04 '20 at 14:31
  • Already enabled it – Spitfire5793 May 04 '20 at 14:51
  • Than I am afraid I can't help you any further. – Rand Random May 04 '20 at 14:53

0 Answers0