MVC5 / C# – Cannot perform runtime binding on a null reference

There are two issues in your code: Consider this: This simplified piece of code throws Cannot perform runtime binding on a null reference since speakers is not defined (null reference). You can fix it by defining speakers in the dynamic ViewBag before the loop: The second issue: speakers in your code is a List, you might want to define ViewBag.speakers as a List<List<string>> and call .Add(speakers) instead … Read more

Checking if an object is null in C#

It’s not data that is null, but dataList. You need to create one with Even better: since it’s a field, make it private. And if there’s nothing preventing you, make it also readonly. Just good practice. Aside The correct way to check for nullity is if(data != null). This kind of check is ubiquitous for reference types; even Nullable<T> overrides the equality operator to … Read more

What is a NullReferenceException, and how do I fix it?

What is the cause? Bottom Line You are trying to use something that is null (or Nothing in VB.NET). This means you either set it to null, or you never set it to anything at all. Like anything else, null gets passed around. If it is null in method “A”, it could be that method “B” passed a null to method “A”. null can have different meanings: Object variables … Read more

What is a NullReferenceException, and how do I fix it?

What is the cause? Bottom Line You are trying to use something that is null (or Nothing in VB.NET). This means you either set it to null, or you never set it to anything at all. Like anything else, null gets passed around. If it is null in method “A”, it could be that method “B” passed a null to method “A”. null can have different meanings: Object variables … Read more