views:

170

answers:

1

Hi Friends,

Here is the code explanation and mapping files of my application

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;



            }


        }

and another method is as follows

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);
            }   
}

and my mapping file is as follows

<?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>

The error i was getting is

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: [NullReferenceException: Object reference not set to an instance of an object.] Cuyahoga.Modules.DocumentManager.DocumentManagerModule.GetNodeCount(Int32 parentfolderID) +84 Cuyahoga.Modules.DocumentManager.Web.DocumentManager.PopulateNodes(IList`1 Values, TreeNodeCollection nodes) +288 Cuyahoga.Modules.DocumentManager.Web.DocumentManager.PopulateRootLevel() +94 Cuyahoga.Modules.DocumentManager.Web.DocumentManager.Page_Load(Object sender, EventArgs e) +121 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.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

Please help me to find the error in this program.Actually i was trying to bind tree view control with a database using NHibernate.As part of implementation i was trying to check if the current parentnode has some children or not.If it has children the bool value is set to true and PopulateOnDemand property is set to true for that node.

A: 

From the looks of it, it would appear that m_SessionFactory has not been initialized within the GetNodeCount method. That is where I would start looking.

Also, you can replace that entire if/else block with return (count > 0). Although, if the method is named GetNodeCount, it really ought to return a count.

Aaronaught
m_SessionFactory was initialized outside the GetNodeCount method.Still the error is same.I updated it in above code
subha
`null` is not initialized. You are getting a `NullReferenceException` because `m_SessionFactory` is in fact `null`. If it is actually being set to an instance of `ISessionFactory` somewhere, it doesn't appear in the code that's posted.
Aaronaught