我对为什么在尝试返回数据集时收到空引用异常感到困惑。
我有一个处理程序和 DBaccess 类,就像在 nTier 架构中一样。
首先,这是我的案例文件处理程序/和访问类的一部分,它们工作正常。我将它们包括在内是因为我不明白为什么这个有效而另一个无效。
public class CaseFileHandler
{
CaseFileAccess caseaccess = null;
public CaseFileHandler()
{
caseaccess = new CaseFileAccess();
}
public DataSet getCaseFileDataset(int caseID)
{
return caseaccess.GetCaseFileDataSet(caseID);
}
public bool UpdateCasefile(CaseFile casefile)
{
return caseaccess.updateCasefile(casefile);
}
}
这是访问类。返回数据集的方法工作得很好。
public class CaseFileAccess
{
const string CONNECTION_STRING = "Data Source = SQLSERVER;Initial Catalog=CaseManager;Integrated Security=SSPI;";
DBManager _DAL = new DBManager(DataProvider.SqlServer, CONNECTION_STRING);
public CaseFile GetCaseFile(int caseID)
{
CaseFile casefile = new CaseFile();
_DAL.Open();
_DAL.CreateParameters(1);
//stuff
return casefile;
}
public DataSet GetCaseFileDataSet(int caseID)
{
_DAL.Open();
_DAL.CreateParameters(1);
_DAL.AddParameters(0, "@CaseID", caseID);
DataSet ds = _DAL.ExecuteDataSet(CommandType.StoredProcedure, "sp_BL_AdjustmentsPage_READ_Case");
_DAL.Close();
return ds;
}
public bool updateCasefile(CaseFile casefile)
{
int result = 0;
_DAL.CreateParameters(56);
//stuff
_DAL.Open();
result = _DAL.ExecuteNonQuery(CommandType.StoredProcedure, "dbo.sp_BL_AdjustmentsPage_UPDATE_Case");
_DAL.Close();
return (result > 0);
}
}
这就是我感到困惑的原因。我创建了一个处理程序和一个访问类来返回要绑定到 asp:dropdown 列表的数据集,并以与案例文件类相同的方式构建这些类
这是处理程序类:
public class ddMenuHandler
{
ddMenuAccess menuaccess = null;
public ddMenuHandler()
{
ddMenuAccess menuaccess = new ddMenuAccess();
}
public DataSet dsCwkrCaseEditing(int cwID)
{
return menuaccess.CwkrsCaseEditing(cwID);
}
}
和访问类:
public class ddMenuAccess
{
const string CONNECTION_STRING = "Data Source = SQLSERVER;Initial Catalog=CaseManager;Integrated Security=SSPI;";
DBManager _DAL = new DBManager(DataProvider.SqlServer, CONNECTION_STRING);
public DataSet CwkrsCaseEditing(int cwID)
{
_DAL.CreateParameters(1);
_DAL.AddParameters(0, "@ID",cwID);
_DAL.Open();
DataSet ds = _DAL.ExecuteDataSet(CommandType.StoredProcedure, "sp_BL_CwkrsActiveCaseEdit");
_DAL.Close();
return ds;
}
}
这是我背后的代码:
protected global::Adjustfiles.CaseFile casefile;
protected global::System.Web.UI.WebControls.DropDownList ddCaseworker;
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
int caseID = Convert.ToInt32(Request.QueryString["caseID"]);
MySession.Current.editCaseID = caseID;
CaseFileHandler cfhandler = new CaseFileHandler();
casefile = cfhandler.getCaseFile(caseID);
DataSet DScase = cfhandler.getCaseFileDataset(caseID);
fv_casefile.DataSource = DScase;
fv_casefile.DataBind();
BindDdCwkrs();
}
}
private void BindDdCwkrs()
{
ddMenuHandler menuaccess = new ddMenuHandler();
DataSet ds = menuaccess.dsCwkrCaseEditing(casefile.CaseWorkerID);
int i = 0;
ddCaseworker.DataSource = ds;
ddCaseworker.DataTextField = "CwName";
ddCaseworker.DataValueField = "CwID";
ddCaseworker.DataBind();
ddCaseworker.SelectedValue = Convert.ToString(casefile.CaseWorkerID);
}
在线ddCaseworker.DataSource = ds;
是我得到空引用异常的地方,menuaccess 是null
. 但为什么?
Source Error:
Line 29:public DataSet dsCwkrCaseEditing(int cwID)
Line 30:{
Line 31: return menuaccess.CwkrsCaseEditing(cwID);
Line 32:}
Line 33:
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=Adjustfiles
StackTrace:
at Adjustfiles.ddMenuHandler.dsCwkrCaseEditing(Int32 cwID) in H:\Visual Studio 2010\Projects\Adjustfiles\Adjustfiles\ddMenuHandler.cs:line 31
at Adjustfiles.adjustFile66EditCase.BindDdCwkrs() in H:\Visual Studio 2010\Projects\Adjustfiles\Adjustfiles\adjustFile66EditCase.aspx.cs:line 45
at Adjustfiles.adjustFile66EditCase.Page_Load(Object sender, EventArgs e) in H:\Visual Studio 2010\Projects\Adjustfiles\Adjustfiles\adjustFile66EditCase.aspx.cs:line 29
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException: