1

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.

6
  • 1
    Can you provide some more code on the creation of the allSpark and ViewBag instance? Commented Feb 21, 2012 at 7:28
  • 1
    How about Foo[i] ? maybe it is null Commented Feb 21, 2012 at 7:28
  • Foo[i] must be null , to my understanding Commented Feb 21, 2012 at 7:29
  • Is Users a collection or just a variable? Can you post your foreach code? Commented Feb 21, 2012 at 7:29
  • 2
    +1 for allspark. Autobots, roll out Commented Feb 21, 2012 at 7:32

4 Answers 4

10

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 = ... ... } 
Sign up to request clarification or add additional context in comments.

3 Comments

+1 Or, of course, ViewBag.Foo[i] = new myDataModelType { Name = ..., Bar = ... };. Or, if performing more complex operations on the item, var item = new myDataModelType(); // operations on item; ViewBag.Foo[i] = item; to save repeatedly indexing into the array.
@AdamRalph: Sure - I wanted to keep to minimal changes for the moment, but those are all good things to consider :)
Thanks! I can't believe I overlooked that.
1

Either ViewBag is null and therefore .Foo[i] does not exist or Foo is null and indexing it is not valid or Foo[i] is null and therefore Foo[i].Name is not valid

Comments

1

If allSpark[i].Users.Name contains a value then it might be referencing the property on the left hand side.

Check to see ifViewBag.Foo exists, and then if it exists at position i.

Comments

0

You must create an instance of ViewBag.Foo[i] before assigning Name to it.

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

allSpark[i].Users.Name has a value, but ViewBag.Foo[i] does not thus the exception.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.