Sign in to follow this  

Help me... Unhandled opperation

This topic is 4839 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay... im reading Tom Miller's Kickstart for Managed Dx9 and i seem to have a little problem compiling the following code...
static void Main() 
		{
			using (Form1 frm = new Form1())
			{
				//Show form, create device, and run the application.
				frm.Show();
				frm.InitializeGraphics();
				Application.Run(frm);
			}
		}


        /// <summary>
        /// Create dx graphic device.
        /// </summary>
		public void InitializeGraphics()
		{
			//Set presentation parameters
			PresentParameters presentParams = new PresentParameters();
        
			presentParams.Windowed = true;
			presentParams.SwapEffect = SwapEffect.Discard;
 
			//Create device.
			device = new Device(0,DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);

			//Create a triangle.
			vb.Created += new EventHandler(this.OnVertexBufferCreate);
			OnVertexBufferCreate(vb, null);
            			
			//Refresh objects here so they can show up at startup.
			obj_toplabel.Refresh();
		}

		/// <summary>
		/// OnPaint is used whenever the window needs to be repainted.
		/// </summary>
		protected override void OnPaint(PaintEventArgs e)
		{
			//Clears the target window to a fixed color.
			device.Clear(ClearFlags.Target, System.Drawing.Color.CadetBlue, 1.0f, 0);

			//Call SetupCamera to set up projection and view.
			SetupCamera();

			//Create a light.
			device.Lights[0].Type = LightType.Directional;
			device.Lights[0].Position = new Vector3();
			device.Lights[0].Diffuse = System.Drawing.Color.White;
			device.Lights[0].Attenuation0 = .2f;
			device.Lights[0].Range = 10000.0f;
			device.Lights[0].Update();
			device.Lights[0].Enabled = true;
            
			//Create the scene.
			device.BeginScene();
			device.VertexFormat = CustomVertex.PositionNormalColored.Format;
			device.SetStreamSource(0, vb, 0);
			device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
			device.EndScene();

			//Update everything.
			device.Present();

			//Invalidating the window.
			this.Invalidate();

		}


		private void SetupCamera()
		{
			device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4,
				this.Width / this.Height, 1.0f, 100.0f);
			device.Transform.View = Matrix.LookAtLH(new Vector3(0,0,5.0f), new Vector3(),
				new Vector3(0,1,0));

			//Enable lights.
			device.RenderState.Lighting = true;

            //Rotate.
			device.Transform.World = Matrix.RotationAxis(new Vector3(angle / ((float)Math.PI * 2.0f), angle / ((float)Math.PI * 4.0f), angle / ((float)Math.PI * 6.0f)), angle / (float)Math.PI); 
			angle += 0.1f;

			//Disable face culling.
			device.RenderState.CullMode = Cull.None;

		}


		//Buffer and Geometry Creation
		private void OnVertexBufferCreate(object sender, EventArgs e)
		{
			VertexBuffer buffer = (VertexBuffer)sender;
						
			CustomVertex.PositionNormalColored[] verts = new CustomVertex.PositionNormalColored[3];
			verts[0].Position = new Vector3(0.0f, 1.0f, 1.0f);
			verts[0].Normal = new Vector3(0.0f, 0.0f, -1.0f);
			verts[0].Color = System.Drawing.Color.White.ToArgb();
			verts[1].Position = new Vector3(-1.0f, -1.0f, 1.0f);
			verts[1].Normal = new Vector3(0.0f, 0.0f, -1.0f);
			verts[1].Color = System.Drawing.Color.White.ToArgb();
			verts[2].Position = new Vector3(1.0f, -1.0f, 1.0f);
			verts[2].Normal = new Vector3(0.0f, 0.0f, -1.0f);
			verts[2].Color = System.Drawing.Color.White.ToArgb();
			
			buffer.SetData(verts, 0, LockFlags.None);
		}


		
        	
	}
}

I get: An unhandled exception of type 'System.NullReferenceException' occurred in Managed Directx.exe Additional information: Object reference not set to an instance of an object. The error is on: vb.Created += new EventHandler this.OnVertexBufferCreate); Why is that? [Edited by - Washu on September 13, 2004 12:44:40 AM]

Share this post


Link to post
Share on other sites
1) use [ source] tags (fixed)
2) where, if ever, is vb set to a value other than null? You need to create the vertex buffer before you can use it.

Share this post


Link to post
Share on other sites
Nope... vb is created before main

public class Form1 : System.Windows.Forms.Form
{
float angle;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Label obj_toplabel;
private Device device = null;
private VertexBuffer vb = null;

Share this post


Link to post
Share on other sites
That doesn't create vb. That initializes it to null. When you try to access it, guess what exception gets thrown? That's right: System.NullReferenceException

Share this post


Link to post
Share on other sites
Well, the thing that i see missing is something like:

vb = new VertexBuffer(...);


So, check the book source code, and see if it's in there somewhere.

Share this post


Link to post
Share on other sites

This topic is 4839 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this