I am getting this error at runtime: object reference not set to an instance of object
my question is that am i using strin开发者_开发知识库gbuilder 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.
empDetails
is currently an array of StringBuilder
s, and empDetails[0]
(the first StringBuilder
in the array) is not instantiated, hence the error you're getting. What you should do is instead of making an array of StringBuilder
s (unless that's what you intend...), you should just make a single StringBuilder
.
StringBuilder empDetails = new StringBuilder();
Then to append you can just do:
empDetails.Append("stringhere");
This line creates an array of 100 null references:
StringBuilder[] empDetails = new StringBuilder[100];
If you want to use empDetails[0]
, you have to assign something to it first:
empDetails[0] = new StringBuilder();
It's not clear to me why you are creating an array of 100 StringBuilders
though. Couldn't you just do this?
htPeople.Add("bellempposreport", "ambreen");
Don't bother assigning this a value:
string num = "ambreen";
as it's going to be lost when you reassign it.
This:
StringBuilder[] empDetails = new StringBuilder[100];
//no need for creating an array of string builders.
Should be this:
StringBuilder empDetails = new StringBuilder();
AND
empDetails.Append("amby");
string num = empDetails.ToString();
http://msdn.microsoft.com/en-us/library/2839d5h5(VS.71).aspx
I think he might want to be pre-sized to 100
StringBuilder empDetails = new StringBuilder(100);
empDetails.Append("stringhere");
Only use stringbuilder if you are combining a large document of text. If you are just joining a few (<10) items simply using string.Format or just + them together such as
string myEmployee = string.Format("First:{0} Last: {1} Middle: {2}", "Stack", "Overflow", "C#");
Thanks everyone. My problem solved using below code:
//Creating stringbuilder array for storing key values
StringBuilder[] empDetails = new StringBuilder[70];
for (int i = 0; i < empDetails.Length; i++)
{
empDetails[i] = new StringBuilder();
}
精彩评论