Help me... Unhandled opperation

Started by
6 comments, last by Washu 19 years, 7 months ago
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]
Advertisement
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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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;
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

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

i dunno man... im jus following the book... and everything worked before making OnVertexBufferCreate...

How can i create it?
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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

You're exactly right.... stupid me.... i really appreciate it man. I'm just learning and i really appreciate you taking time to help me.

Thanks again Washu.
*shrug* It's what they don't pay me for [grin]

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

This topic is closed to new replies.

Advertisement