我很困惑为什么在尝试返回数据集时我得到一个空引用异常。
我在nTier架构中有一个处理程序和DBaccess类。
首先,这是我的casefile处理程序/和访问类的一部分工作正常。 我把它们包括在内是因为我无法理解为什么这种方法有效而另一方则不然。
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列表的数据集,并以与case文件类相同的方式构造类
这是处理程序类:
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:
答案 0 :(得分:6)
您将ddMenuAccess设置为本地函数变量而不是类变量:
该行:
ddMenuAccess menuaccess = new ddMenuAccess();
更改为:
this.menuaccess = new ddMenuAccess();
应该按预期工作。 (“这。”可选,但有助于澄清)
public class ddMenuHandler
{
ddMenuAccess menuaccess = null;
public ddMenuHandler()
{
this.menuaccess = new ddMenuAccess();
}
public DataSet dsCwkrCaseEditing(int cwID)
{
return menuaccess.CwkrsCaseEditing(cwID);
}
}