当前位置: 代码迷 >> ASP.NET >> 用户代码未处理NullReferenceException: 未将对象引用设立到对象的实例
  详细解决方案

用户代码未处理NullReferenceException: 未将对象引用设立到对象的实例

热度:8450   发布时间:2013-02-25 00:00:00.0
用户代码未处理NullReferenceException: 未将对象引用设置到对象的实例
代码如下:
  DateTime selDate = (DateTime)DatePicker1.SelectedDate; 
  DataSet ds = new DataSet();
  //ds = bllcount.GetList(" CT_ISOK=1 and CT_ISMS=0");
  ds = bllcount.GetList(" CT_ISOK=1 and CT_ISMS=0", selDate);

  if (ds.Tables.Count > 0)
  {
  Grid1.DataSource = ds.Tables[0];
  Grid1.DataBind();
  }
每次把数据集绑定到grid的时候就会出错。

堆栈跟踪: 


[NullReferenceException: 未将对象引用设置到对象的实例。]
  ExtAspNet.WindowField.GetColumnValue(GridRow row) +1021
  ExtAspNet.GridRow.DataBindRow() +275
  ExtAspNet.Grid.DataBindRow(Int32 rowIndex, Object rowObj) +172
  ExtAspNet.Grid.DataBindToDataTable(DataTable dataTable) +131
  ExtAspNet.Grid.DataBind() +403
  WebFrameWork.webPage.TJpage.SSFY.BindGridData() in G:\供热收费\20120618\HRSF\WebFrameWork\webPage\TJpage\SSFY.aspx.cs:43
  WebFrameWork.webPage.TJpage.SSFY.Page_Load(Object sender, EventArgs e) in G:\供热收费\20120618\HRSF\WebFrameWork\webPage\TJpage\SSFY.aspx.cs:29
  System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
  System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
  System.Web.UI.Control.OnLoad(EventArgs e) +99
  System.Web.UI.Control.LoadRecursive() +50
  System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627


网上看到很多类似的情况,可是都不太一样,请高手指教~~~~(>_<)~~~~ 



------解决方案--------------------------------------------------------
遇到这种问题基本上可以归结为:
1:本质上的错误:
object a;//a是Null对象
protected void Page_Load(object sender, EventArgs e)
{
a.ToString();//调用一个Null对象的方法
}
2:通常性的错误:
示例1:一个过滤某些字符的函数:
public static string FilterValue(string value)
{
string[] filterChar = new string[] { "\'", ",", ">", "<", "=", ";", "\"", "--" };
for (int i = 0; i < filterChar.Length; i++)
{
value = value.Replace(filterChar[i], "");
}
return value.Trim(' ');
}

这个函数比如容易看的出:如果value传进来为Null的时候,就等于Null.Replace被调用,就出现了上面的错误。

因此,通常的,在函数的首行,都会对value进行:if(!string.IsNullOrEmpty(value)) 一下。
  相关解决方案