You must Sign In to post a response.
  • Category: .NET

    Object reference not set to an instance of an object error

    Hi
    please help me solve this error:
    In line 45 I keep getting this error: Object reference not set to an instance of an object
    here is the code:

    namespace WindowsFormsApplication5
    {
    public partial class Form1 : Form
    {
    Bitmap bt;
    public Form1()
    {
    InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
    DialogResult dr = openFileDialog1.ShowDialog();


    if (dr == DialogResult.OK)
    {
    Bitmap bt = new Bitmap(openFileDialog1.FileName);
    pictureBox1.Image = bt;
    }
    }

    private void button2_Click(object sender, EventArgs e)
    {
    Bitmap btout = new Bitmap(Width, Height);

    for (int x = 0; x < bt.Width; x++)
    {
    for (int y = 0; y < bt.Height; y++)
    {
    Color p;
    p = bt.GetPixel(x, y);

    int a = p.A;
    int r = p.R;
    int g = p.G;
    int b = p.B;

    int avg = (r + g + b) / 3;

    btout.SetPixel(x, y, Color.FromArgb(avg, avg, avg));

    }
    }
    pictureBox2.Image = btout;

    btout.Save("d:\\visual exports\\grayscale.png");
    }

    I will be Thankful.
  • #762630
    Hi

    In button_click2 the value for width and height are null, so you are getting object reference not set exception.

    Use below code to overcome that exception.

    public partial class Form1 : Form
    {
    protected internal Bitmap bt;

    public Form1()
    {
    InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
    DialogResult dr = openFileDialog1.ShowDialog();


    if (dr == DialogResult.OK)
    {
    Bitmap bt = new Bitmap(openFileDialog1.FileName);

    pictureBox1.Image = bt;

    }
    }

    private void button2_Click(object sender, EventArgs e)
    {
    try
    {
    int w = pictureBox1.Image.Width;
    int h = pictureBox1.Image.Height;

    Bitmap bmp = new Bitmap(pictureBox1.Image);

    for (int x = 0; x < w; x++)
    {
    for (int y = 0; y < h; y++)
    {

    Color p;
    //pictureBox1.Image = bt;
    p = bmp.GetPixel(x, y);

    int a = p.A;
    int r = p.R;
    int g = p.G;
    int b = p.B;

    int avg = (r + g + b) /3 ;
    Bitmap btout = new Bitmap(w, h);
    btout.SetPixel(x, y, Color.FromArgb(avg, avg, avg));
    pictureBox2.Image = btout;

    btout.Save("d:\\Image\\sridhar.jpg");
    }
    }
    }
    catch (Exception ec)
    {

    MessageBox.Show("" + ec);
    }
    }
    }

    Sridhar Thota.
    Editor: DNS Forum.

  • #762641
    The root cause problem of this type of error is, you are trying to access null reference in that case you may face this type of error. Before access the value check the condition whether object contains value or not. If not then skip the assignment else assign value.

    EX:

    if(value!=null)
    //assign value


    To analyse the issue in your code blocks better to debug the program then only you can get where it's get strucked or refer stack trace for more details.

    Hope this will helpful to you...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #762693

    Hai Danya,
    This is very common error which you will get when the object is null and you are trying to convert it to some other type.
    So before converting, first you need to check whether the object is having value or not.
    There is a small issue in your code at the below line:
    var btout = new Bitmap(_bt.Width, _bt.Height);

    Rest all is well. I just formatted your code as per the coding standards. Please follow the below:

    public partial class Form1 : Form
    {
    private Bitmap _bt;

    public Form1()
    {
    InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
    var dr = openFileDialog1.ShowDialog();
    if (dr != DialogResult.OK) return;
    _bt = new Bitmap(openFileDialog1.FileName);
    pictureBox1.Image = _bt;
    }

    private void button2_Click(object sender, EventArgs e)
    {
    var btout = new Bitmap(_bt.Width, _bt.Height);
    for (var x = 0; x < _bt.Width; x++)
    {
    for (var y = 0; y < _bt.Height; y++)
    {
    var p = _bt.GetPixel(x, y);
    int r = p.R;
    int g = p.G;
    int b = p.B;
    var avg = (r + g + b)/3;
    btout.SetPixel(x, y, Color.FromArgb(avg, avg, avg));
    }
    }
    pictureBox2.Image = btout;
    btout.Save("D:\\visualexports\\grayscale.png");
    }
    }

    Hope it will be helpful to you.


    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #762765
    Thank you Guys so much for your help :)


This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.