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 7852045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:19:35+00:00 2026-06-02T19:19:35+00:00

I get a NullReferenceException was unhandled by user code error with the following code

  • 0

I get a “NullReferenceException was unhandled by user code” error with the following code in my View when I pass in a null value via my controller. There are situations where I want to pass in a null value, but I do not want an error thrown when this happens. What should I change my code to?

Originally my code was:

@foreach (var item in Model.MyModelStuff)
{
    <tr>
        <td>
                @Html.DisplayFor(modelItem => item.Bla.Title)
        </td>
    <tr>
}

I have tried the following with no success:

@foreach (var item in Model.MyModelStuff.Where( item => item.MyModelStuff != null))
etc. . . 

How do I change the code so that it will handle null without throwing an error? I’ve read I may need to be returning an empty collection of my model (?), how would I go about doing that – if it is indeed the necessary thing to do?

  • 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-02T19:19:36+00:00Added an answer on June 2, 2026 at 7:19 pm

    If my understanding is correct your collection is null.

    A collection should never be null, like you said you should return an empty collection instead and prevent your collection to be corrupted not exposing the real collection:

    public IList<Employee> Employees
    {
        get; 
        private set;
    }
    

    And initialize your collection inside the constructor

    this.Employees = new List<Employee>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get NullReferenceException was unhandled by usercode in below code. if (item[Area] != null)
I am trying to update a user information and get the following error: Object
I get the following error: Client does not support authentication protocol requested by server;
I get the memberOf property for my user using this code: DirectorySearcher search =
I am struggling with the code when I run it I get this error.
I am new to programming, and have come across the following error: System.NullReferenceException was
I get a list of all users in my database with the following code:
Whenever I run my program, I get: NullReferenceException was unhandled, Object Reference not set
I have the following code in a strongly-typed View in my application: <td> <label
How do I handle NullReferenceException for the below statement as I get null exception

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.