1

I know that this is one of the most encountered error but I am really struggling to get around it. i have a property on my Controller: private readonly ISelectListFactory _selectFactory; and a method that called to populate the viewbag private void PopulateLists() { var continent = _selectFactory.GetItems(); } and the interface

public interface ISelectListFactory
{
    IEnumerable<SelectListItem> GetItems();

}

and in the controller constructor I have the following:

public LocationController(ISelectListFactory selectFactory)
    {
        _selectFactory = selectFactory;
     }

but I am getting this error Object reference not set to an instance of an object and not sure how to overcome it.

Regards

4
  • Do you have a line of code where an instance of an object gets created and placed into _selectFactory? Commented Aug 11, 2013 at 17:16
  • @DavidTansey there is a method that implement the ISelectListFactory where the GetItems methods is defined, but strange enough if I put a breakpoint on GetItems, I don't get there and this is strange to me Commented Aug 11, 2013 at 17:44
  • look closely at the code in the answer of Darin below. Commented Aug 11, 2013 at 17:49
  • @DavidTansey: you are right mate thanks was blinded by the readonly property.Cheers Commented Aug 11, 2013 at 17:59

1 Answer 1

1

Make sure you have instantiated this _selectFactory variable somewhere. Like for example:

_selectFactory = new SomeConcreteSelectListFactory();

or if you are using dependency injection you might configure your DI framework to inject it into the constructor:

public class HomeController: Controller
{
    private readonly ISelectListFactory _selectFactory;
    public HomeController(ISelectListFactory selectFactory)
    {
        _selectFactory = selectFactory;
    }

    ... some controller actions where you could use the _selectFactory field
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.