【发布时间】:2009-12-31 04:13:02
【问题描述】:
这是我的应用程序的代码解释和映射文件
private void PopulateNodes(IList<Folder> Values, TreeNodeCollection nodes)
{
foreach (Folder r in Values)
{
TreeNode tn = new TreeNode();
tn.Text = r.folderName.ToString();
tn.Value = r.folderID.ToString();
nodes.Add(tn);
bool val = _documentManagerModule.GetNodeCount((int)r.folderID);
tn.PopulateOnDemand = val;
}
}
另外一种方法如下
private ISessionFactory m_SessionFactory = null;
public bool GetNodeCount(int parentfolderID)
{
ISession session = m_SessionFactory.OpenSession();
int count = (int)session.CreateQuery("select count(f.parentID) from Folder f where f.parentID = ?").SetParameter(0, parentfolderID.ToString()).UniqueResult();
try
{
if (count > 0)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
throw new Exception("Unable to get ChildFolders ", ex);
}
}
而我的映射文件如下
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace ="Cuyahoga.Modules.DocumentManager"
assembly ="Cuyahoga.Modules.DocumentManager">
<!-- Mappings for class 'Folder' -->
<class name="Folder" table="cm_folders" lazy="false">
<!-- Identity mapping -->
<id name="folderID">
<column name="folder_ID" />
<generator class="native" />
</id>
<!-- Simple mappings -->
<property name="folderName" column ="folder_name" />
<property name="parentID" column ="parent_ID" />
<!-- one-to-many mapping: Document-->
<bag name="Documents" table ="cm_documents" cascade="save-update" lazy="false">
<key column="folder_ID" />
<one-to-many class="Document" />
</bag>
<!--Reflexive associations-->
<many-to-one name="ParentFolder" class ="Folder" column ="parent_ID" cascade ="none"/>
<bag name="ChildFolder" table ="cm_folders" cascade="save-update" inverse ="true" lazy="false">
<key column="parent_ID" />
<one-to-many class="Folder" />
</bag>
</class>
</hibernate-mapping>
我得到的错误是
对象引用未设置为对象的实例。
说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。
来源错误: 在执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常起源和位置的信息。
堆栈跟踪: [NullReferenceException:对象引用未设置为对象的实例。] Cuyahoga.Modules.DocumentManager.DocumentManagerModule.GetNodeCount(Int32 parentfolderID) +84 Cuyahoga.Modules.DocumentManager.Web.DocumentManager.PopulateNodes(IList`1 值,TreeNodeCollection 节点)+288 Cuyahoga.Modules.DocumentManager.Web.DocumentManager.PopulateRootLevel() +94 Cuyahoga.Modules.DocumentManager.Web.DocumentManager.Page_Load(对象发送者,EventArgs e)+121 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp,对象 o,对象 t,EventArgs e)+14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(对象发送者,EventArgs e)+35 System.Web.UI.Control.OnLoad(EventArgs e) +99 System.Web.UI.Control.LoadRecursive() +50 System.Web.UI.Control.LoadRecursive() +141 System.Web.UI.Control.LoadRecursive() +141 System.Web.UI.Control.LoadRecursive() +141 System.Web.UI.Control.LoadRecursive() +141 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
请帮我找出这个程序中的错误。实际上我正在尝试使用 NHibernate 将树视图控件与数据库绑定。作为实现的一部分,我试图检查当前父节点是否有一些子节点.如果它有子节点,则布尔值设置为 true,并且该节点的 PopulateOnDemand 属性设置为 true。
【问题讨论】:
-
GetNodeCount返回一个布尔值是相当具有欺骗性的。我建议您将函数重命名为HasNodes或从函数返回实际节点数。
标签: c# nhibernate exception treeview controls