passing mouse clicks to line algorithm
<p>I wrote bresenham algorithm for </p>

<blockquote>
  <p><code>0&lt;Angular coefficient&lt;1</code></p>
</blockquote>

<p>I don't know much about <strong>graphics</strong> in C#,I realized that for drawing <strong>pixles</strong> I can use the function <strong>Fillrectangel</strong> with coordinate 1,1 </p>

<p>I wanted to write my code then when clicking the mouse on panel and in two positions draw me a line from <strong>x0,y0 to xEnd,yEnd</strong></p>

<p>so here is my code which has exception</p>

<p><img src="http://i.stack.imgur.com/XWCNR.png" alt="enter image description here"></p>

<p><em>Null reference exception was unhandled
object reference not set to an instance of the object</em>
this exception is in line  e.Graphics.FillRectangle(new SolidBrush(grad1), x, y, 1, 1);</p>

<p><strong>I think the problem is that object e is Null and I should new it but how?</strong> </p>

<p>How can I correct my code so as to draw Line?</p>

<pre><code>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

    namespace WindowsFormsApplication3
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void panel1_Paint(object sender, PaintEventArgs e)
            {
                Line l=new Line();
                l.LineBres(Cursor.Position.X, Cursor.Position.Y, Cursor.Position.X, Cursor.Position.Y);

            }
        }
    }
    public class Line
    {
        System.Windows.Forms.DrawItemEventArgs e;
        Color grad1 = Color.FromArgb(165, 194, 245);
        public void LineBres(int x0, int y0, int xEnd, int yEnd)
        {
            int dx = xEnd - x0;
            int dy = yEnd = y0;
            int p = 2 * dy - dx;
            int twoDy = 2 * dy;
            int twoDyMinusDx = 2 * (dy - dx);
            int x, y;
            if (x0 &gt; xEnd)
            {
                x = xEnd;
                y = yEnd;
                xEnd = x0;
            }
            else
            {
                x = x0;
                y = y0;
            }

            e.Graphics.FillRectangle(new SolidBrush(grad1), x, y, 1, 1);
            while (x &lt; xEnd)
            {
                x++;
                if (p &lt; 0)
                    p += twoDy;
                else
                {
                    y++;
                    p += twoDyMinusDx;
                }
                e.Graphics.FillRectangle(new SolidBrush(grad1), x, y, 1, 1);
            }
        }

    }
</code></pre>
