0

We were getting a NullReferenceException from line 135 until I added the if statement on line 134.

It shouldn’t have been necessary because of the check on line 124.

I couldn’t reproduce it locally. But it happened every time on live for certain scenarios. This extension method is called from the SearchResultsViewModel constructor, but I wouldn’t think that would matter.

Am I missing something obvious?

enter image description here

This is how I call the extension method

ParseParentCategory(SearchKey)

This is how the SearchResultsViewModel is defined

Public Class SearchResultsViewModel
    Inherits ViewModelBase
    Public ReadOnly _ConstructedProperly As Boolean
    Public Property SearchKey As String
    Public Property ParentCategoryId As Integer = -1
    Public Property ParentCategoryName As String

Public Sub New(Request As HttpRequestBase, pPartiallyPopulatedStupidInputModel As SearchResultsViewModel, pSearchBy As IProductsSearch.SearchBy)
rojobo
  • 476
  • 4
  • 16
  • Why is this flagged as C#? – Necoras Aug 11 '16 at 15:26
  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Matt Rowland Aug 11 '16 at 15:27
  • @Necoras its not anymore – rojobo Aug 11 '16 at 15:28
  • 1
    Can you give us more detail about the definition of a SearchResultsViewModel? – simonalexander2005 Aug 11 '16 at 15:28
  • @MattRowland read the question then comment nonsense. – rojobo Aug 11 '16 at 15:29
  • @simonalexander2005 sure ill update the question in a sec – rojobo Aug 11 '16 at 15:29
  • 1
    Have you tried debugging and checking when this object has lost its value? I'll assume no since you say you can't reproduce locally. Then do you know what is different between your environment and the live one which could be the cause of this? – Noémie Lord Aug 11 '16 at 15:31
  • 4
    Have you tried this locally under load? i.e. could another thread (request) be resetting the value of `pThis` between the check and the call? – stuartd Aug 11 '16 at 15:32
  • 1
    @MattRowland Not a duplicate at all... – Noémie Lord Aug 11 '16 at 15:32
  • 1
    @rojobo Is this application multithreaded? – vbnet3d Aug 11 '16 at 15:36
  • @rojobo: re: "read the question then comment nonsense" - be nice. You may not think it is a duplicate. You may be right. But referring to it as nonsense doesn't help anybody here. If you think its not a duplicate explain why. There really are a lot of questions on here that say "I'm getting this NRE..." that the majority of them are closed as duplicates of that. – Chris Aug 11 '16 at 15:40
  • Also you say you can reliably recreate it in certain scenarios but then don't detail what those scenarios are. This seems like crucial information to be sharing with us! – Chris Aug 11 '16 at 15:42
  • @vbnet3d Its an asp MVC searchbar ? It’s passed byVal so I thought it wouldnt affect it but I may be wrong – rojobo Aug 11 '16 at 15:42
  • @Chris you are right. I spent time trying to make this question as detailed as possible...those "scenarios" mean it happened when the user searched for certain terms but I still couldn't reproduce it searching for those exact terms – rojobo Aug 11 '16 at 15:45
  • @stuartd good point no I havent tried it like that I just ran one instance via the VS debugger will do and post results thanks! – rojobo Aug 11 '16 at 15:46
  • 1
    Everyone is over-thinking it. It is not a threading problem, each request will get it's own view model. It is simple, `ParentCategoryId` is null depending on the POST. This is simply a NRE duplicate. – Crowcoder Aug 11 '16 at 15:47
  • 2
    @rojobo I would suggest adding some logging into this function in several different places, and then compare them on the live system. On a slightly different note, this is an extension method, so your null ref exception should be happening before this method ever gets called. – vbnet3d Aug 11 '16 at 15:47
  • @Crowcoder I believe the OP is saying that `pThis` is null, not `ParentCategoryId`. @rojobo, is that correct? – vbnet3d Aug 11 '16 at 15:49
  • @vbnet3d I will certainly consider this thanks for the feedback – rojobo Aug 11 '16 at 15:49
  • Is this part of a very slow process? The app could recycle right in the middle of a process. – the_lotus Aug 11 '16 at 15:49
  • 1
    @vbnet3d yes it is correct I dont know why crowcoder says its very simple if it was I wouldnt be wasting peoples time – rojobo Aug 11 '16 at 15:50
  • @Crowcoder I honestly dont think you read the question im making sure pthis isnot nothing before i access its properties – rojobo Aug 11 '16 at 15:51
  • 1
    @vbnet3d: You can call an extension method on a null object (in C# at least). The compiler just rewrites the call to the static method passing the object as the first parameter. In theory if the method never looked at the first parameter it need never throw a null reference exception! – Chris Aug 11 '16 at 15:59
  • 1
    @rojobo: if it is happening for specific search terms then it sounds like a data issue. Try replicating the data on your dev environment (or just lookign to see if there is anything wrong with it). – Chris Aug 11 '16 at 16:00
  • 2
    @Chris That is true - normally this would be an instance method, but since it is written as an extension method, it is static in VB.NET as well. – vbnet3d Aug 11 '16 at 16:02
  • @simonalexander2005 Added some more I dont know if thats enough though – rojobo Aug 11 '16 at 16:19

0 Answers0