Click here to Skip to main content
11,805,035 members (61,246 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C#
Why i getting error : Error! Object reference not set on instance of an object. c#

private StringBuilder htmlMessageBody(DataGridView dg)
    StringBuilder strB = new StringBuilder();
    //create html & table
    strB.AppendLine("<html><body><center><table border='1' cellpadding='0' cellspacing='0'>");
    //cteate table header
    for (int i = 0; i < dg.Columns.Count; i++)
        strB.AppendLine("<td align='center' valign='middle'>" + dg.Columns[i].HeaderText + "</td>");
    //create table body
    for (int i = 0; i < dg.Rows.Count; i++)
        foreach (DataGridViewCell dgvc in dg.Rows[i].Cells)
            strB.AppendLine("<td align='center' valign='middle'>" + dgvc.Value.ToString() + "</td>");
    //table footer & end of html file
    return strB;


private void bMail_Click(object sender, EventArgs e)
    MailMessage myMessage = new MailMessage();
        myMessage.From = "";
        myMessage.To = "";
        myMessage.Cc = "";
        myMessage.BodyEncoding = Encoding.UTF8;
        myMessage.BodyFormat = MailFormat.Html;
        myMessage.Body = htmlMessageBody(dataGridView2).ToString();
        myMessage.Subject = "Test";
        myMessage.Fields.Add("", "0");
        myMessage.Fields.Add("", "userName");
        myMessage.Fields.Add("", "password");
        SmtpMail.SmtpServer = "";
        MessageBox.Show("Message sent!");
    catch (Exception ex)
        MessageBox.Show("Error! " + ex.Message);
Posted 16-Apr-13 3:15am
ant 385275
Marcus Kramer at 16-Apr-13 9:17am
On what line of code is the error happening. You need to use your debugger and step through the code at which point it will at least tell you what line. Tell us that and then we can help you.
Member 10381340 at 12-Nov-13 7:01am
public void LoadCombo()
con = new Connection("Cartridges");
foreach (DataRow row in con.DataS.Tables["Cartridges"].Rows)

Object reference not set to an instance of an object... is on the foreach loop (foreach keyword is highlighted), plz help
Pheonyx at 16-Apr-13 9:33am
When do you instantiate SmtpMail ? from the code you have shown it doesn't appear to be defined/instantiated, just used.
babu saravanan at 17-Apr-13 0:10am
can you please give ti in more detail where error occured
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Object reference not set to an instance of an object

This error happens when you try to use a property or call a method of an object that is null. More details: here[^]

A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line. Check the objects of that line and see if any one is null and you are trying to use that objects property. Handle the same.
Rate this: bad
Please Sign up or sign in to vote.

Solution 5

You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Good luck,
Rate this: bad
Please Sign up or sign in to vote.

Solution 6

Can't get the source above to compile but I did notice the following :

To send the mail, you need to do something like this :

SmtpClient client = new SmtpClient("");

I suspect that your exception may be coming from the dataGridView2 passed into the htmlMessageBody method. Step through the code and make sure that the variable 'dg' is not null when it enters that method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 315
1 CPallini 210
2 Richard Deeming 200
3 Sergey Alexandrovich Kryukov 168
4 CHill60 150
0 OriginalGriff 3,255
1 Maciej Los 1,990
2 KrunalRohit 1,907
3 CPallini 1,855
4 Richard MacCutchan 1,227

Advertise | Privacy | Mobile
Web02 | 2.8.151002.1 | Last Updated 16 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100