2

I get a NullReferenceException when try to run a view. I don't see the code that cause this problem. Can someone explain the problem? thanks

This is the Model class:

public class Catalogus: ICatalogus
{
    private readonly DbSet<Materiaal> materialen;
    private IEnumerable<Materiaal> materialenTest;
    private Firma firma;

    public Catalogus()
    {
        firma = new Firma("hh", "[email protected]");
        materialenTest = new Materiaal[] { new Materiaal(5, 0, "1", "test", "test", "ts", firma, "wereldbol", "wereldbol", "lol", 0, true) };
    }

    public IEnumerable<Materiaal> VindAlleMaterialen()
    {
        return materialenTest.OrderBy(m => m.Naam);
    }

    public IEnumerable<Materiaal> ZoekOpTrefwoord(string trefwoord)
    {
        IEnumerable<Materiaal> gefilterdMaterialen = materialenTest.Where(mat => mat.GetType().GetProperty("naam").GetValue(this).Equals(trefwoord));

        return gefilterdMaterialen;
    }
} 

The controller with the NullRef exception:

This line cause the problem.

IEnumerable materialen = catalogus.VindAlleMaterialen().OrderBy(m => m.Naam).ToList();

public class CatalogusController : Controller
{
    private ICatalogus catalogus;

    public CatalogusController() { }

    public CatalogusController(ICatalogus catalogus)
    {
        this.catalogus = catalogus;
    }

    public ActionResult Index()
    {
        IEnumerable<Materiaal> materialen = catalogus.VindAlleMaterialen().OrderBy(m => m.Naam).ToList();

        return View(materialen);
    }
}
4
  • Looks like either it calls the default constructor not injecting catalogus, or one of the elements is null or has a null Naam. Commented Mar 22, 2016 at 18:17
  • Having a controller with interface parameters in the constructor tells me you're probably using some kind of DI framework, maybe Ninject - in that scenario, there's something wrong with your setup. Maybe just remove the default constructor and see if that works, and if not, consult your documentation on your DI framework to ensure that it's configured properly. Commented Mar 22, 2016 at 18:19
  • where is this line IEnumerable materialen = catalogus.VindAlleMaterialen().OrderBy(m => m.Naam).ToList(); in the code posted above??? Commented Mar 22, 2016 at 18:31
  • @Xoce웃Пepeúpa 4th row from the end, although it has a template parameter that was missing in the text. Commented Mar 22, 2016 at 18:36

2 Answers 2

7

Your default constructor public CatalogusController does not create an catalogus instance. When you then execute catalogus.VindAlleMaterialen().OrderBy(m => m.Naam).ToList(); it results in a NullReferenceException because catalogus is null.

If your overloaded constructor IS being called (probably not the case) you should validate the incoming parameter.

public CatalogusController(ICatalogus catalogus)
{
    if(catalogus == null) throw new ArgumentNullException("catalogus");
    this.catalogus = catalogus;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Without the default constructor I receive "MissingMethodException". says I don"t have a parameterles constructor. that's the reason I try to add the default constructor.
@Dave_888 - This means you are probably using a DI (dependency injection) or IoC framework. You probably did not register your Catalogus with it and specify that ICatalogus instances can be replaced with that type. If you want more help on that you should include that information (what DI framework and the registration code).
@Dave_888 - if this is not the case then you should create a new instance of catalogus in the default constructor and assign it to your private field catalogus. How ever you look at it, if your catalogus instance is null you will get a NullReferenceException as soon as you try to access it's properties/methods.
oke,thx for your comments. I will ask if I need further guidance.
1

It looks like someone could contruct a CatalogusController without passing in an ICatalogus object. This will cause a NullReferenceException when someone calls controller.Index():

// Create a controller object using the default constructor
CatalogusController controller = new CatalogusController();

// this causes a NullReferenceException because controller.catalogus is null
controller.Index();

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.