Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8884163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:59:54+00:00 2026-06-14T20:59:54+00:00

I have class ElementRelation { … } and class ElementRelationCollection : System.Collections.ObjectModel.ObservableCollection<ElementRelation> { …

  • 0

I have class ElementRelation { ... } and class ElementRelationCollection : System.Collections.ObjectModel.ObservableCollection<ElementRelation> { ... }

And I have this code:

ElementRelationCollection a = ...;
if (a == null)
    throw new Exception("a is null(???)"); // this exception is never thrown
try
{
    foreach (ElementRelation relation in a) // exception is thrown here
    { ... } // never thrown here
}
catch (NullReferenceException ex)
{
    string message = "Something is null here. a.Count: " + a.Count;
    IEnumerator<ElementRelation> enumerator = a.GetEnumerator();
    message += ", enumerator is " + (enumerator == null ? "null" : "not null");
    throw new Exception(message, ex);
}

I can see from the logs that this code sometimes throws an Exception with message
“Something is null here. a.Count: 9, enumerator is not null”. When this starts happening it continues top happen on every page load until I iisreset.

The innerexception of course is a System.NullReferenceException and it has this stacktrace:

at MyNamespace.MyClass.MyMethod() in c:\path\MyClass.cs:line 74

where line 74 is the line that says foreach (ElementRelation relation in a)

Why does it throw this exception?

Edit:

The collection is sometimes updated by a background thread. I thought this could not cause worse problems than a failed iteration but it turns out the whole collection becomes corrupted.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:59:55+00:00Added an answer on June 14, 2026 at 8:59 pm

    The ObservableCollection<T> class is not thread-safe. Modifying it from multiple threads can corrupt the internal state to such an extent that it becomes impossible to modify or enumerate the instance.

    You will randomly see exceptions such as NullReferenceException and IndexOutOfRangeException – and that’s the best possible result! In other cases, the changes to the collection could be silently dropped.

    You will either need to wrap your access to the list in a lock, possibly using the ReaderWriterLockSlim class, or switch to a thread-safe collection. For .NET 4.0, the System.Collections.Concurrent namespace has several possibilities.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class MyForm which inherited from QMainWindow . Here's my code: std::auto_ptr<MyForm> pForm(new
I have class like this below shown. which contains the shopping items where the
I have class that looks like this: class A { public: class variables_map vm
I have class B that has class A, this is simplified version of class
I have class GUI so I can create object like this: GUI g1 =
I have class that extends BroadcastReceiver and gets all new sms. When I get
I have class dot and have html code like that: <span class=dot>Text1</span> <span class=dot>Text2</span>
i have class like this DrillDownAppAppDelegate.h PictureCell.h RootViewController.h SlideShowViewController.h DrillDownAppAppDelegate.m PictureCell.m RootViewController.m SlideShowViewController.m i
I have class in server side and I want use method of this class
I have class which has Map, but when I try to presist this entity,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.