7

I am building a 3D application with Silverlight 5. I have a DrawingSurface which calls a method. However, when I add a navigation:Frame to my XAML, I then get an error thrown.

Here is the method in question:

private void DrawingSurface_Draw(object sender, DrawEventArgs e)
{

    GraphicsDevice device = GraphicsDeviceManager.Current.GraphicsDevice;

    device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer,
                 new Microsoft.Xna.Framework.Color(0, 0, 0, 0), 10.0f, 0);

    device.SetVertexBuffer(_vertexBuffer);
    device.SetVertexShader(_vertexShader);
    device.SetPixelShader(_pixelShader);
    device.Textures[0] = _texture;

    device.SamplerStates[0] = SamplerState.LinearClamp;
    device.DrawPrimitives(PrimitiveType.TriangleList, 0,
                          _vertexBuffer.VertexCount / 3);
    device.SetVertexShaderConstantFloat4(0, ref _viewProjection);

    e.InvalidateSurface();

}

The error is at the line device.DrawPrimitives(PrimitiveType.TriangleList, 0, _vertexBuffer.VertexCount / 3); . The error is that "NullReferenceException was unhandled by user code." It does not occur without the navigation:Frame.

Silvermind
  • 5,271
  • 2
  • 21
  • 39
Jake
  • 3,051
  • 4
  • 16
  • 36
  • 1
    I'm not familiar with this. But error says that `PrimitiveType.TriangleList` and/or `_vertexBuffer.VertexCount` are null. Maybe they are not populated at that time when you try to access them. – Davor Mlinaric Oct 23 '14 at 19:25
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ben Black Nov 07 '14 at 16:30
  • Please provide a stack trace and any inner exceptions. – Nikhil Jan 14 '15 at 05:53

1 Answers1

0

From this snippet of code the only conclusion we can make is that "vertexBuffer" is empty or null when you enter this method. That or some varaibles used locally by the "DrawPrimitive" method.

I have no idea how this variable is populated, but overall it is good practice to always check for null or empty when you are not 100% sure things are populated in advance.

jimmy
  • 1,763
  • 2
  • 16
  • 28