   What is the problem?
  Hi,I am new to DirectX and i am trying to write simple application.When i run it i have an exception - "Object not referance".I am using DirectX 9 and C#.Here is the code:using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Drawing.Imaging;using Microsoft.DirectX;using Microsoft.DirectX.Direct3D;namespace TestDirectX{/// <summary>/// Summary description for Form1./// </summary>public class frmDirectX : System.Windows.Forms.Form{// Global variables for this projectDevice device = null;   VertexBuffer vertexBuffer = null;IndexBuffer indexBuffer = null;int m_Width;int m_Height;int[,] m_HeightData;   /// <summary>/// Required designer variable./// </summary>private System.ComponentModel.Container components = null;public frmDirectX(){//// Required for Windows Form Designer support//InitializeComponent();// Set the initial size of our formthis.ClientSize = new System.Drawing.Size(400,300);CreateHeightData();}/// <summary>/// Clean up any resources being used./// </summary>protected override void Dispose( bool disposing ){if( disposing ){if (components != null) {components.Dispose();}}base.Dispose( disposing );}#region Windows Form Designer generated code/// <summary>/// Required method for Designer support - do not modify/// the contents of this method with the code editor./// </summary>private void InitializeComponent(){this.components = new System.ComponentModel.Container();this.Size = new System.Drawing.Size(300,300);this.Text = "Form1";}#endregion/// <summary>/// The main entry point for the application./// </summary>[STAThread]static void Main() {using (frmDirectX frm = new frmDirectX()){try{if (!frm.InitializeGraphics()) {MessageBox.Show("Could not initialize Direct3D.  This tutorial will exit.");return;}frm.Show();// While the form is still valid, render and process messageswhile(frm.Created){frm.Render();Application.DoEvents();}}catch (Exception e){MessageBox.Show(e.Message);}}}private void CreateHeightData(){Bitmap bitmap = (Bitmap)Bitmap.FromFile(Environment.CurrentDirectory + @"\Height.bmp");m_Height = 6;//bitmap.Height;m_Width = 6;//bitmap.Width;m_HeightData = new int[m_Height, m_Width];for (int h = 0 ; h < m_Height ; h ++){for (int w = 0 ; w < m_Width ; w ++){m_HeightData[w, h] = bitmap.GetPixel(w, h).B / 50;}}}public bool InitializeGraphics(){try{// Now  setup our D3D stuffPresentParameters presentParams = new PresentParameters();presentParams.Windowed=true;presentParams.SwapEffect = SwapEffect.Discard;device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);device.RenderState.CullMode = Cull.None;OnCreateDevice(device, null);//OnResetDevice(device, null);//device.RenderState.FillMode = FillMode.WireFrame;return true;}catch (DirectXException){ return false; }}private void Render(){if (device == null) return;//Clear the backbuffer to a blue color device.Clear(ClearFlags.Target, System.Drawing.Color.Black, 1.0f, 0);//Begin the scenedevice.BeginScene();    // Set up the world, view, and projection matrices.SetupMatrices();// Rendering of scene objects can happen heredevice.SetStreamSource(0, vertexBuffer, 0);device.Indices = indexBuffer;device.VertexFormat = CustomVertex.PositionColored.Format;//device.DrawPrimitives(PrimitiveType.TriangleList, 0, (m_Width * m_Height) / 3);device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, m_Width * m_Height, 0, m_Width * m_Height);//End the scenedevice.EndScene();device.Present();}public void OnCreateDevice(object sender, EventArgs e){Device dev = (Device)sender;    // Now create the vertex buffervertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored), m_Width * m_Height, dev, Usage.WriteOnly | Usage.Dynamic, CustomVertex.PositionColored.Format, Pool.Default);vertexBuffer.Created += new System.EventHandler(this.OnCreateVertexBuffer);this.OnCreateVertexBuffer(vertexBuffer, null);indexBuffer = new IndexBuffer(typeof(int), (m_Width - 1) * (m_Height - 1) * 3, dev,Microsoft.DirectX.Direct3D.Usage.WriteOnly, Pool.Default);indexBuffer.Created += new EventHandler(this.OnCreateIndexBuffer);this.OnCreateIndexBuffer(indexBuffer, null);}public void OnResetDevice(object sender, EventArgs e){Device dev = (Device)sender;// Turn off culling, so the user sees the front and back of the triangledev.RenderState.CullMode = Cull.None;// Turn off Direct3D lighting, since object provides its own vertex colorsdev.RenderState.Lighting = false;}public void OnCreateVertexBuffer(object sender, EventArgs e){VertexBuffer vb = (VertexBuffer)sender;GraphicsStream stm = vb.Lock(0, 0, 0);CustomVertex.PositionColored[] verts =new CustomVertex.PositionColored[m_Width * m_Height];//verts[0].X=150; verts[0].Y=50; verts[0].Z=0.5f; verts[0].Rhw=1; //verts[0].Color = System.Drawing.Color.Aqua.ToArgb();////verts[1].X=250; verts[1].Y=250; verts[1].Z=0.5f; verts[1].Rhw=1;//verts[1].Color = System.Drawing.Color.Brown.ToArgb();////verts[2].X=50; verts[2].Y=250; verts[2].Z=0.5f; verts[2].Rhw=1;//verts[2].Color = System.Drawing.Color.LightPink.ToArgb();for (int h = 0 ; h < m_Height ; h ++){for (int w = 0 ; w < m_Width ; w ++){verts[w + (h * m_Height)].Position = new Vector3(w, h, m_HeightData[h, w]);verts[w + (h * m_Height)].Color = System.Drawing.Color.White.ToArgb();}}stm.Write(verts);vb.Unlock();}public void OnCreateIndexBuffer(object sender, EventArgs e){IndexBuffer ib = (IndexBuffer)sender;int [] indcies = new int[(m_Width - 1) * (m_Height - 1) * 6];int s = 0;for (int y = 0 ; y < m_Height - 1; y ++){for (int x = 0 ; x < m_Width - 1; x ++){indcies[s++] = (m_Width * y) + x;indcies[s++] = (m_Width * y) + x + m_Width + 1;indcies[s++] = (m_Width * y) + x + m_Width;indcies[s++] = (m_Width * y) + x;indcies[s++] = (m_Width * y) + x + 1;indcies[s++] = (m_Width * y) + x + m_Width + 1;}}ib.SetData(indcies, 0, Microsoft.DirectX.Direct3D.LockFlags.None);}private void SetupMatrices(){device.Transform.View = Matrix.LookAtLH( new Vector3( 0.0f, 3.0f,-5.0f ), new Vector3( 0.0f, 0.0f, 0.0f ), new Vector3( 0.0f, 1.0f, 0.0f ) );device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, 1.0f, 1.0f, 100.0f );}}}   
  1° Use [ source ] [/ source ] tags around your code to make it more readable2° What line is the exception thrown from, originally, and what does the call stack look like?   