1

Object instance not set to an instance of an object error in Asp.Net

I am getting this error every now in then in my ASP.Net web application and cannot understand how. The stack trace leads me to this code and the line is one or other of the calls to the Add method of the HashTable. The code is in a module and the Hash Table is declared as a private variable in that module.

I have added the Debug.Asserts but so far these have only pushed the line number of the error further down to another line where the Add method is called. I am not overriding the Hashtable 's Add method in a derived class. The parameters beginning "gstr" are string constants instantiated in another module.

How can it get past one call to the Add method (which means _hshBaseTables cannot be Null) and then come up with this exception?

<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)> _
Friend Module modFieldAliases

#Region "Private Data"

'Tables created by InitFieldAliasHashTable and destroyed by calling UnInitFieldAliasHashTable
'private variable to hold lookup table for field aliases.
Private _hshFieldAliases As System.Collections.Hashtable 'use this to obtain underlying field information given the field alias
Private _hshTableAliases As System.Collections.Hashtable 'use this to obtain the base table and column given table alias
Private _hshBaseTables As System.Collections.Hashtable 'use this to obtain table alias from base table
Private _hshBaseColumns As System.Collections.Hashtable 'use this to obtain field alias from base table and column pair  

...

If _hshBaseTables IsNot Nothing Then

    _hshBaseTables = System.Collections.Specialized.CollectionsUtil.CreateCaseInsensitiveHashtable(50)

    Debug.Assert(_hshBaseTables IsNot Nothing)
    _hshBaseTables.Add("Alias", gstrALIAS)
    Debug.Assert(_hshBaseTables IsNot Nothing)
    _hshBaseTables.Add("BlobData", gstrBLOBDATA)
    Debug.Assert(_hshBaseTables IsNot Nothing)
    _hshBaseTables.Add("ContactLink", gstrCONTACTLINK)
    Debug.Assert(_hshBaseTables IsNot Nothing)
    _hshBaseTables.Add("CustomForms", gstrCUSTOMFORMS)...

Here is the stack trace:

at modFieldAliases.InitFieldAliasHashTable() in C:\Ajexus 4.59\CriteriaSet\FieldAliases.vb:line 2701
at Ajexus.CriteriaSet._initialize() in C:\Ajexus 4.59\CriteriaSet\CriteriaSet.vb:line 41
at Ajexus.CriteriaSet..ctor() in C:\Ajexus 4.59\CriteriaSet\CriteriaSet.vb:line 46
at Model.GetData(String method, Dictionary`2 data) in C:\Ajexus 4.59\Ajexus MVP Framework\Model.vb:line 424
at Ajexus.Framework.ReportsHelper.GetUserCriteriaListContents(String strFieldAlias, CriteriaSet csPredefinedReportCriteria, CriteriaSet csReportCriteria) in C:\Ajexus 4.59\Ajexus MVP Framework\ReportsHelper.vb:line 560
at Ajexus.Framework.Presenters.Summary2ViewPresenter._fillUserCriteriaList(CriteriaSet csPredefinedReport, String strFieldAlias, String strFieldLabel, String strDisplayMember, String strValueMember) in C:\Ajexus 4.59\Ajexus MVP Framework\Presenters\Summary2.aspx.presenter.vb:line 173
at Ajexus.Framework.Presenters.Summary2ViewPresenter.OnFillUserCriteriaFieldList(Object sender, AjexusFieldEventArgs e) in C:\Ajexus 4.59\Ajexus MVP Framework\Presenters\Summary2.aspx.presenter.vb:line 836
at Ajexus.Web.Views.Summary2.fvwUserCriteria_PreRender(Object sender, EventArgs e) in C:\Ajexus 4.59\AjexusWeb\Summary2.aspx.vb:line 80
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Additional Information The _hshBaseTables variable is Nothing when the exception is caught and the stack trace usually indicates a different line than previously. But it is always after the check for Nothing and after nearly identical lines of code where the exception has not been thrown. This suggests doesn't it that it must have been set to Nothing by another thread in between this thread's calls to the Add method? hshBaseTables is a private variable in a Friend Module which is included also in another component used by my application.

  • Are you not able to put a debugger on it to see what these values are? – Joe Enos Jul 22 '13 at 21:40
  • 1
    Is it possible that one of the gstr variables is null? – Andrew Walters Jul 22 '13 at 21:43
  • @AndrewWalters HashTable is perfectly happy to add and store null references. One of those variables could be null, but that wouldn't cause an exception... at least not one that pointed here. – Joel Coehoorn Jul 22 '13 at 21:46
  • 1
    You're right, I must have been thinking of something else. Maybe post the stack trace – Andrew Walters Jul 22 '13 at 21:51
  • Could it be possible the hashtable is being modified in another thread and you're running into some collision while initializing it here? – Andrew Walters Jul 22 '13 at 21:53
  • @AndrewWalters I think it must be something like that but I'm not sure how. I've posted a stacktrace but am not sure how much help it will be. – Stephen Phillip Baldwin Jul 22 '13 at 23:06
  • @JoeEnos Sorry for not replying sooner. I have tried catching the exception - I can get it to happen if I play around for long enough - but then when it is caught I can't see any problems. I am using VS2010. I can't set next statement from within Catch but if I step out and come back in the exception does not occur again. – Stephen Phillip Baldwin Jul 22 '13 at 23:16
  • @StephenPhillipBaldwin Is the _hshBaseTables public/static? If it is it might be worth pulling all the references to it and seeing if there's any way two pages could be accessing it at the same time. Just curious, what happens in that code snippet if the hashtable isn't null? – Andrew Walters Jul 22 '13 at 23:38
  • @AndrewWalters Thanks for your comments Andrew. _hshBaseTables is declared Private and is in a module which is declared Friend - the module itself is used also in another component. _hshBaseTable is Nothing when the exception occurs I have discovered - apologies Joe. But it is occurring on a line according to the stacktrace which is not always the same but is always after the check for nothing and after some prior calls to the Add method using _hshBaseTable which suggests some other thread has set it to nothing in the meantime?. I'll look into your suggestion Andrew – Stephen Phillip Baldwin Jul 23 '13 at 00:53
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Jul 23 '13 at 00:55
  • @JohnSaunders Thanks John. _hshBaseTables is Null when the exception is thrown but it is becoming null quite suddenly - it is not null on the previous line which confused me. I am fairly new to ASP.Net and to this site - perhaps the title needs to be changed. It appears to me it may be the result of another thread changing it to null while the current thread is still using it but I am not clear how this is happening. Your link does not cover this – Stephen Phillip Baldwin Jul 23 '13 at 01:21
  • Please show the declaration of `_hshBaseTables`. If it is `Shared` (`static` in C#), then that's your problem. Each HTTP request is served on a separate thread. You almost certainly have more than one thread active at once. Without locking or a better design, you'll have threads stepping on each other. – John Saunders Jul 23 '13 at 01:31
  • @JohnSaunders I've changed the title - not sure. I'm assuming now its caused by another thread changing the value and I'll look about for answers along that line. – Stephen Phillip Baldwin Jul 23 '13 at 01:44
  • `Module` in VB makes all of the members `Shared`. Do not use it in an ASP.NET or other server-based application. There is only one copy of each of those variables for ***all users*** and ***all requests*** – John Saunders Jul 23 '13 at 01:51
  • @AndrewWalters Once initialized the `Hashtable` values don't change. The mistake, as you helped me to realize, was to allow a class that uses this module to set the Hashtable to Nothing when that class is disposed because in ASP.Net some other thread might still be using it. If I prevent this it seems ok. – Stephen Phillip Baldwin Jul 23 '13 at 03:58
  • @JohnSaunders It sounds like you should have posted an answer instead of putting the answer in the comments. Comments get deleted, they are hard to follow, and they aren't as useful as a full fledged answer. – George Stocker Jul 24 '13 at 01:07
  • @GeorgeStocker: as an answer, it's not very complete. I was hoping that the comment would send the OP in the right direction, and that someone with more free time would post an answer which is more complete. – John Saunders Jul 24 '13 at 01:08

2 Answers2

1

The answer as pointed out by Andrew and John in the comments is that the code above is contained in a VB.Net Module. This means all members are Shared. There is only one copy available for all users and all requests. At some point this variable is being set to Nothing while another request thread is still trying to use it. It suits my purposes to have only one copy available to all users in this case but I must ensure _hshBaseTables is not set to Nothing.

0

Your code is returning nothing from following statement

_hshBaseTables = System.Collections.Specialized.CollectionsUtil.CreateCaseInsensitiveHashtable(50)

Check and make sure that its does not return nothing

Jalpesh Vadgama
  • 13,653
  • 19
  • 72
  • 94
  • No, if that were true then it would fail on the first line after and that it is not the case. The problem is I believe that I am setting `_hshBaseTables` to Nothing when I am disposing of a class that uses this module when it is still being used by another thread. Thanks anyway for your time. – Stephen Phillip Baldwin Jul 23 '13 at 21:26