1

I have a method that is called from other methods. This method creates a configuration object and an ISessionFactory:

private static ISessionFactory sessionFactory()
    {

        Configuration myConfig = null;
        ISessionFactory mySessFac = null;
        try
        {
            myConfig = new Configuration();
            if (myConfig != null)
            {
                myConfig.Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "hibernate.cfg.xml"));
                mySessFac = myConfig.BuildSessionFactory();
            }
        }
        catch (Exception e)
        {
            throw;
        }

        return mySessFac;

    }

The problem is that I get an error at myConfig.Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "hibernate.cfg.xml"));

The error is:

Object reference not set to an instance of an object

Even though this is a straight forward error I have difficulties to solve it. Here is a method that uses the above method:

 public static IList<Course> RetrieveAllCourses()
    {
        IList<Course> cList = null;
        try
        {
            using (ISession mySess = sessionFactory().OpenSession())
            {
                ICriteria criteria = mySess.CreateCriteria<Course>();
                cList = criteria.List<Course>();
            }
        }
        catch (Exception e)
        {
            throw;
        }
        return cList;
    }

My hibernate.cfg.xml is set to embedded resource

    <?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="dialect">NHibernate.Dialect.MsSql2012Dialect</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">Data Source=Mnemonics;User ID=Mnmncs;Password=mnmncs;Initial Catalog=database-name;Integrated Security=true</property>
    <property name="show_sql">true</property>
    <mapping assembly="RManageSystemService"/>
  </session-factory>
</hibernate-configuration>

And the mapping file is also set to embedded resource:

    <?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="RManageSystemService"
                   namespace="RManageSystemService.orm_rman_systm">
  <class name="Course" table="dbo.Courses" lazy="false">
    <id name="CCode" column="ccode">
      <generator class="identity"/>
    </id>
    <property name="CName" column="cname"/>
    <property name="Credits" column="credits"/>
  </class>
</hibernate-mapping>

So what is the problem with my code? I would appreciate some help.

2 Answers 2

2

The problem was one of the mapping file that had duplicated property mapping. This error was in some way overridden by the try catch statement in the method sessionFactory() which only returned an object error.

Sign up to request clarification or add additional context in comments.

Comments

0

I think that problem could be in the path or XML structure, but I am not experienced with NHibernate framework, so I am not sure about funcionality of Configure() method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.