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

and

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)
{

Matrix[] transforms = new Matrix[model.Bones.Count];
model.CopyAbsoluteBoneTransformsTo(transforms);

// 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];
effect.Parameters["World"].SetValue(world);
effect.Parameters["View"].SetValue(view);
effect.Parameters["Projection"].SetValue(projection);
effect.Parameters["LightDirection"].SetValue(lightDir);
effect.Parameters["LightViewProj"].SetValue(lightViewProjection);

}
// Draw the mesh
mesh.Draw();
}
}


Any help would be greatly appreciated, thanks.

• Have you tried stepping through the code? It looks to me like your effect.Techniques[techniqueName] is null. – John McDonald Mar 20 '12 at 15:54
• 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. – Kurt Ricci Mar 20 '12 at 16:21
• @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 – Kurt Ricci Mar 20 '12 at 16:24
• 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. – 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.

 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.

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.