views:

143

answers:

1

Following is the code I am using. Seems pretty simple to me. But I get a NullReferenceException at the last line , the return statement. Here is the stack trace:

FailedSystem.NullReferenceException: Object reference not set to an instance of an object. at NHibernate.Criterion.Junction.ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary2 enabledFilters) at NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetWhereCondition(IDictionary2 enabledFilters) at NHibernate.Loader.Criteria.CriteriaJoinWalker..ctor(IOuterJoinLoadable persister, CriteriaQueryTranslator translator, ISessionFactoryImplementor factory, CriteriaImpl criteria, String rootEntityName, IDictionary2 enabledFilters) at NHibernate.Loader.Criteria.CriteriaLoader..ctor(IOuterJoinLoadable persister, ISessionFactoryImplementor factory, CriteriaImpl rootCriteria, String rootEntityName, IDictionary2 enabledFilters) at NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results) at NHibernate.Impl.CriteriaImpl.List(IList results) at NHibernate.Impl.CriteriaImpl.List()

 public List<Person> Search(string keyword)
        {
            ICriteria criteria = session.CreateCriteria(typeof (Person));
            Disjunction disjunction = Restrictions.Disjunction();
            {
            // In here, there is a whole lot of business logic adding around 20 conditions     to the disjunction

            }

            criteria.Add(disjunction);
            return criteria.List<Person>() as List<Person>;

        }
A: 

The clue is in your stack trace.

It's only at your return statement that the criteria is evaluated to produce a SQL statement.

FailedSystem.NullReferenceException: 
Object reference not set to an instance of an object.
at NHibernate.Criterion.Junction.ToSqlString(
    ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary2 enabledFilters) 
at NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetWhereCondition(
    IDictionary2 enabledFilters)

Comment out all your criteria and reintroduce them in small groups until you find the one that's causing the problem.

Most likely you're referencing a property that does't exist, or isn't mapped, or providing a null to a criteria that doesn't support it.

Bevan