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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:22:11+00:00 2026-05-30T08:22:11+00:00

I have some C# code in my ASP.NET MVC3 project that’s throwing an exception

  • 0

I have some C# code in my ASP.NET MVC3 project that’s throwing an exception claiming:

Cannot perform runtime binding on a null reference

The line of code throwing the exception is:

ViewBag.Foo[i].Name = allSpark[i].Users.Name;

This is the code block:

ViewBag.Foo = new myAllSparkModelType[allSpark.Length];
for (int i = 0; i < allSpark.Length; i++)
{
    ViewBag.Foo[i].Name = allSpark[i].Users.Name;
    ...
}

When I set a breakpoint and inspect allSpark[i].Users.Name, it definitely has a value (e.g. “Fred”).

If I comment out this line of code, the next line (which is similar code) then throws the same exception.

So the problem is either that allSpark[i].Users.Name is a null reference (Which I’ve confirmed isn’t) or I can’t just use the ViewBag like I am. If it’s the latter, I’m confused as I thought that the ViewBag could be used this way.

  • 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-05-30T08:22:12+00:00Added an answer on May 30, 2026 at 8:22 am

    Look at this code:

    ViewBag.Foo = new myDataModelType[allSpark.Length];
    for (int i = 0; i < allSpark.Length; i++)
    {
        ViewBag.Foo[i].Name = ...
        ...
    }
    

    ViewBag.Foo is initialized, but each element of ViewBag.Foo will be null, assuming myDataModelType is a class. When you create an array, each element is initialized to the default value of the element type – and for reference types, that default value is null.

    You need to create a new object for each element:

    for (int i = 0; i < allSpark.Length; i++)
    {
        ViewBag.Foo[i] = new myDataModelType();
        ViewBag.Foo[i].Name = ...
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have some legacy ASP.NET code that detects if a request is secure, and
I have a normal asp.net page containing some code that I want to measure
I have an asp.net mvc3 project, it has some reports in aspx web pages.
I'm writing a web page in ASP.NET. I have some JavaScript code, and I
I have some C# / asp.net code I inherited which has a textbox which
I have some code in asp.net ( kindly given by someone else ) to
I'm using jqModal in my ASP.net MVC 3 project. I have some buttons where
We have some files stored in sql database. On an ASP.NET MVC3 form, we
I have some code for starting a thread on the .NET CF 2.0: ThreadStart
I have some code in a javascript file that needs to send queries back

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.