Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7748161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:42:39+00:00 2026-06-01T10:42:39+00:00

I wrote bresenham algorithm for 0<Angular coefficient<1 I don’t know much about graphics in

  • 0

I wrote bresenham algorithm for

0<Angular coefficient<1

I don’t know much about graphics in C#,I realized that for drawing pixles I can use the function Fillrectangel with coordinate 1,1

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

so here is my code which has exception

enter image description here

Null reference exception was unhandled
object reference not set to an instance of the object

this exception is in line e.Graphics.FillRectangle(new SolidBrush(grad1), x, y, 1, 1);

I think the problem is that object e is Null and I should new it but how?

How can I correct my code so as to draw Line?

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 > xEnd)
            {
                x = xEnd;
                y = yEnd;
                xEnd = x0;
            }
            else
            {
                x = x0;
                y = y0;
            }

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

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:42:40+00:00Added an answer on June 1, 2026 at 10:42 am

    Here is what you need to change:

    Add mouse click event for panel and change your Line code a bit – remove System.Windows.Forms.DrawItemEventArgs e; and pass Graphics of panel with panel1.CreateGraphics();. Here is the code:

    private int firstX, firstY;//store coordinates of first click
    private bool firstClick = true;
    private void panel1_MouseClick(object sender, MouseEventArgs e)
    {
        if (firstClick)
        {
            firstX = e.X;
            firstY = e.Y;
            firstClick = false;
        }
        else
        {
            Line l = new Line();
            l.LineBres(firstX, firstY, e.X, e.Y, panel1.CreateGraphics());
            firstClick = true;
        }
    }
    
    public class Line
    {
        private Color grad1 = Color.FromArgb(165, 194, 245);
    
        public void LineBres(int x0, int y0, int xEnd, int yEnd, Graphics e)
        {
            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 > xEnd)
            {
                x = xEnd;
                y = yEnd;
                xEnd = x0;
            }
            else
            {
                x = x0;
                y = y0;
            }
    
            e.FillRectangle(new SolidBrush(grad1), x, y, 1, 1);
            while (x < xEnd)
            {
                x++;
                if (p < 0)
                    p += twoDy;
                else
                {
                    y++;
                    p += twoDyMinusDx;
                }
                e.FillRectangle(new SolidBrush(grad1), x, y, 1, 1);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a program that forks some processes with fork(). I want to kill
I wrote a routine to remove pounds and ids from sharepoint fields that worked
I wrote a C program in Linux that mallocs memory, ran it in a
I wrote a windows service using VB that read some legacy data from Visual
I wrote myself a little downloading application so that I could easily grab a
I wrote a component that displays a filename, a thumbnail and has a button
I wrote a simple tool to generate a DBUnit XML dataset using queries that
I wrote an application that currently runs against a local instance of MySql. I
I wrote a utility for photographers that I plan to sell online pretty cheap
I wrote an objective-C class that needs to notify another class, so i defined

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.