Hi, I am getting this error at runtime: object reference not set to an instance of object

my question is that am i using stringbuilder array correctly here. Because I am new in C#. and i think its the problem with my stringbuilder array. Below is the code:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Services; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 
using System.Text; 
using System.Web.Script.Serialization; 
using System.Web.Script.Services; 
using System.Collections; 
 
 
public partial class Testing : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
 
    } 
 
 
    [WebMethod] 
    public static string SendMessage() 
    { 
        try 
        { 
            al2c00.ldap ws = new al2c00.ldap(); 
            Hashtable htPeople = new Hashtable(); 
 
            //DataTable dt = ws.GetEmployeeDetailsBy_NTID("650FA25C-9561-430B-B757-835D043EA5E5", "john"); 
 
            StringBuilder[] empDetails = new StringBuilder[100]; 
 
 
            string num = "ambreen"; 
            empDetails[0].Append("amby"); 
            num = empDetails[0].ToString(); 
 
            htPeople.Add("bellempposreport", num); 
 
 
            JavaScriptSerializer jss = new JavaScriptSerializer(); 
 
            string output = jss.Serialize(htPeople); 
            return output; 
        } 
        catch(Exception ex) 
        { 
            return ex.Message + "-" + ex.StackTrace; 
        } 
    } 
}

please reply me what i am doing wrong here.

Your assumption is correct.

StringBuilder[] empDetails = new StringBuilder[100];

This line establishes an array of StringBuilder references, but you still need to create instances of objects for each array element (*or at least any element you intend to actually use).

for (int i = 0; i < empDetails.Length; i++)
{
    empDetails[i] = new StringBuilder();
}

Then you will be able to access member functions and properties of the empDetails[x] objects.

Edited 6 Years Ago by apegram: n/a

This place cause an error that Object reference not set to an instance of an object..i dono how to rectify my error

SqlCommand cmd = new SqlCommand("DELETE from regCompany WHERE reg_Id='" +lbl.Text+"'", db);

This question has already been answered. Start a new discussion instead.