I get below error when I try to add controller to my ASP.NET MVC 3 intranet project
Object reference not set to an instance of object
I am following an article http://sumitmaitra.wordpress.com/2011/11/13/fun-with-signalr/ to develop SignalR application.
I doubt that after installing SignalR something went wrong but not very sure about it.
Edit
Model code:
public class BlogPost
{
[Key]
public int Id { get; set; }
[Required]
public string Title { get; set; }
public string Post { get; set;}
}
DbContext:
namespace SignalR.Models
{
public class BlogPostContext : DbContext
{
public DbSet<BlogPost> BlogPosts { get; set; }
}
}
Controller:

I don’t know anything about SignalR but i’m pretty sure the problem has something to do with your view trying to render HTML for properties of a null object.
I had a skim through the article and cannot see any reference to the actual database that stores the data and no code was provided to generate the database from your code (and the post’s author is not clear on wether you will be connecting to an existing database or generating one).
See here for example of mapping your code to an EXISTING database.
If you don’t already have an existing database then you need to tell your Entity Framework code to generate one; This article will show you how to do that (the stuff you need is towards the bottom of the post).
Now, looking at the way you have set up your entity class and assuming you haven’t already done any of the things mentioned in the two articles, Entity Framework should be creating a database for you automatically (which is the default behaviour – not sure what happens if you don’t have SQL Server Express or greater installed though). Have you checked to see if you have a database in your SQL server instance? It should be called BlogPostContext. Fill this with some data and things should now work.
So why are you getting this problem? At the moment you either have
This means your controllers are either
The result you will get back from both scenarios is a null object. This null object is then passed to your views which are then trying to iterate over the null object and generating your exception.
You can verify this in two ways.
For solution 1 something like the following in your view:
For solution two you need to replace the data retrieval code in your controller. So remove any code like this;
and replace it with something like this;
Note: These two solutions are for demonstrating the problem to you and you should revert to using the data retrieval code once you have correctly setup your database.