i am trying to draw a line between two points
like this:

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Graphics MyGraphics = e.Graphics;
            Pen NewPen = new Pen(Color.Red,5);
            MyGraphics.DrawLine(NewPen,StartDraw,EndDraw);
        }

it gives me an error:
Object reference not set to an instance of an object.
what have i done wrong ?

Dani AI

Generated

The NullReferenceException happened because the Paint handler was being invoked incorrectly. Calling Form1_Paint directly from your button click (without a real PaintEventArgs) means e or e.Graphics is null — that produces the error. was right to ask if StartDraw/EndDraw were initialized, and is also correct that the points must be defined. ’s later use of CreateGraphics() will paint immediately, but that drawing is temporary and will vanish on the next repaint (resize, minimize/restore, etc.).

Correct pattern (robust and persistent)

  • Store the line coordinates in fields (or a collection).
  • Change those coordinates on button click or mouse events, then call Invalidate() (or Refresh()) to request a repaint.
  • Do all actual drawing inside the Paint handler (or by overriding OnPaint) and dispose GDI+ objects (use using for Pen).
  • Enable double-buffering to avoid flicker.

Example (modern, persistent approach):

// fields
private List<(Point start, Point end)> _lines = new List<(Point start, Point end)>();

// add a line and request repaint (call from button click or mouse handlers)
private void AddLine(Point s, Point t)
{
    _lines.Add((s, t));
    Invalidate(); // do NOT call the Paint method directly
}

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    if (_lines.Count == 0) return;
    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    using (var pen = new Pen(Color.Red, 3))
    {
        foreach (var (start, end) in _lines)
            e.Graphics.DrawLine(pen, start, end);
    }
}

Also enable optimized double buffering in your form constructor:

SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
UpdateStyles();

Quick checks if something still fails: verify the Paint event is wired, ensure you never call the Paint handler with null args, run with a breakpoint to see which reference is null, and avoid CreateGraphics() for persistent UI painting.

Recommended Answers

All 4 Replies

What about StartDraw and EndDraw? Is they initialized properly?

i dont have those functions.
what i'm trying to do is paint line on event.
i want the line to be painted only after i push a button.
so on my button click handler i just called
Form1_Paint.
i guess this is wrong.

ok here is the answare:

void paintArc(Point Start,Point End)
        {
            Graphics graph = this.CreateGraphics();
            Pen penCurrent = new Pen(Color.Red,3);
            graph.DrawLine(penCurrent,Start,End);
        }

origional code not working

Graphics graph = this.CreateGraphics();
Pen penCurrent = new Pen(Color.Red,3);
graph.DrawLine(penCurrent,Start,End);

working code

Graphics graph = this.CreateGraphics(); 
Pen penCurrent = new Pen(Color.Red, 3); 
graph.DrawLine(penCurrent, 0,0, 100,100); 

the computer does not know the definitions start and end
it is possible to define them as points first
Point new as follows

Graphics graph = this.CreateGraphics(); 
Pen penCurrent = new Pen(Color.Red, 3);
Point Start = new Point(0, 0);
Point End = new Point(100, 100);
graph.DrawLine(penCurrent,Start, End);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.