I've been trying to implement shadows in my game and I've been following quite a few tutorials online, mainly Riemers, but I'm always getting the same 2 errors when I'm drawing my models and setting the parameters from the effect file. The errors are:

This method does not accept null for this parameter. Parameter name: value


Object reference not set to an instance of an object.

So I've then downloaded a sample and just replaced my model with the one found in the sample and the same errors occur. I this find very strange as it works with his model. I'm wondering if the problem is with my models (I made them myself). The errors start to occur after the second foreach loop.

void DrawModel(Model model, bool createShadowMap)
    string techniqueName = createShadowMap ? "CreateShadowMap" : "DrawWithShadowMap";

    Matrix[] transforms = new Matrix[model.Bones.Count];

    // Loop over meshs in the model
    foreach (ModelMesh mesh in model.Meshes)
        // Loop over effects in the mesh
        foreach (Effect effect in mesh.Effects)
            // Set the currest values for the effect
            effect.CurrentTechnique = effect.Techniques[techniqueName];

            if (!createShadowMap)
        // Draw the mesh

Any help would be greatly appreciated, thanks.

  • 2
    \$\begingroup\$ Have you tried stepping through the code? It looks to me like your effect.Techniques[techniqueName] is null. \$\endgroup\$ – John McDonald Mar 20 '12 at 15:54
  • \$\begingroup\$ Can you pastebin your shader code? \$\endgroup\$ – Luis Estrada Mar 20 '12 at 16:21
  • \$\begingroup\$ hey..ye i debugged the code before and it says its null, but what I don't get is why does it work with his model and not with mine. Even with the other tutorials I keep getting the same errors. \$\endgroup\$ – Kurt Ricci Mar 20 '12 at 16:21
  • \$\begingroup\$ @LuisEstrada, here is the shader code, but as I've mentioned before I'm using the shader code found in the sample and all I'm doing is just changing his model to mine, and that is when it gives me the errors: pastebin.com/M5N6ivTh \$\endgroup\$ – Kurt Ricci Mar 20 '12 at 16:24
  • 1
    \$\begingroup\$ Make sure your project settings for graphics are set to HiDef, and not Reach, the shadow mapping demo doesn't work in Reach settings because XNA 4.0 Reach profile doesn't allow for a render target format that supports the def that the shadow mapping technique needs. \$\endgroup\$ – Nic Foster Mar 22 '12 at 4:33

I had a hunch, which I've explained in the comments but I'm pretty certain I've figured it out.

The ShadowMappingExample uses a custom pipeline to add the ShadowMap effect to all the models. This way you can access the shadowmapping shader via

 foreach (ModelMesh mesh in model.Meshes)
                // Loop over effects in the mesh
                foreach (Effect effect in mesh.Effects)

You have let Visual Studio know that you want your own models to be imported using the ShadowMappingExample pipeline and not using the default model pipeline that just applies BasicEffect as shader.

To do this set ContentProcessor to CustomEffectModelProcessor instead of just Model.

Here's a screenshot, it should be in the lower right corner of Visual Studio when you've selected your model.

enter image description here

Now to explain why you got the null reference error: this happens because the default processor applies the BasicEffect shader, which doesn't have the technique which you are trying to set as active.

Hope this explains everything.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.