-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Nullexception
PostPosted: Thu Apr 05, 2007 2:23 pm 
Newbie

Joined: Thu Mar 29, 2007 6:42 am
Posts: 10
I'm new to hibernate.
I have following code on page load
protected void Page_Load(object sender, EventArgs e)
{ISession session = NHibernateHelper.GetCurrentSession();
IList cntList = session.CreateCriteria(typeof(contacts)).List();
GridView1.DataSource = cntList;
DataBind();
session.Close();
}
Hibernate version:1.2.0

Mapping documents:
file name : contacts.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="nhTest.contacts" table="contacts" dynamic-update="true">
<id name="contactID" column="contactID" type="Int32" unsaved-value="0">
<generator class="identity" />
</id>
<property name="Name" column="Name" type="String" length="50" />
<property name="address" column="address" type="String" length="100" />
<property name="city" column="city" type="String" length="50" />
<property name="state" column="state" type="String" length="50" />
<property name="country" column="country" type="String" length="50" />
<property name="sex" column="sex" type="String" length="1" />
<property name="email" column="email" type="String" length="50" />
<property name="phone" column="phone" type="String" length="15" />
<property name="mobile" column="mobile" type="String" length="15" />
<property name="company" column="company" type="String" length="50" />
</class>

</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():



Full stack trace of any exception that occurs:
[NullReferenceException: Object reference not set to an instance of an object.]
nhTest.NHibernateHelper.GetCurrentSession() in e:\WebSites\Testing\nhtest1\App_Code\NHibernateHelper.cs:30
_Default.Page_Load(Object sender, EventArgs e) in e:\WebSites\Testing\nhtest1\Default.aspx.cs:19
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +13
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +45
System.Web.UI.Control.OnLoad(EventArgs e) +80
System.Web.UI.Control.LoadRecursive() +49
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3743




Name and version of the database you are using:MSsql2005 express

_________________
Sharique uddin Ahmed Farooqui
(Asp.Net,C# developer, IT Consultant)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 06, 2007 11:05 pm 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
Your NHibernateHelper class is throwing the exception but the code isn't here so not sure how to help you.
Perhaps your session factory hasn';t been instantiated?

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 07, 2007 3:57 am 
Newbie

Joined: Thu Mar 29, 2007 6:42 am
Posts: 10
Code for helper class

Code:
namespace nhTest
{
    /// <summary>
    /// Summary description for NHibernateHelper
    /// </summary>
    public sealed class NHibernateHelper
    {
        private const string CurrentSessionKey = "nhibernate.current_session";
        private static ISessionFactory sessionFactory;

        public  NHibernateHelper()
        {
            Configuration cfg = new Configuration();
            cfg.AddFile("contacts.hbm.xml");
            sessionFactory = cfg.BuildSessionFactory();
        }

        public static ISession GetCurrentSession()
        {
            HttpContext context = HttpContext.Current;
            ISession currentSession = context.Items[CurrentSessionKey] as ISession;
            if (currentSession == null)
            {
                currentSession = sessionFactory.OpenSession();
                context.Items[CurrentSessionKey] = currentSession;
            }
            return currentSession;
        }

        public static void CloseSession()
        {
            HttpContext context = HttpContext.Current;
            ISession currentSession = context.Items[CurrentSessionKey] as ISession;
            if (currentSession == null)
            {
                // No current session
                return;
            }
            currentSession.Close();
            context.Items.Remove(CurrentSessionKey);
        }

        public static void CloseSessionFactory()
        {
            if (sessionFactory != null)
            {
                sessionFactory.Close();
            }
        }
    }
}

_________________
Sharique uddin Ahmed Farooqui
(Asp.Net,C# developer, IT Consultant)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 07, 2007 10:54 am 
Regular
Regular

Joined: Mon Mar 20, 2006 10:49 pm
Posts: 59
You are calling a static method which references sessionFactory which has never been set since the constructor is not static.

_________________
Mike Abraham


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 15, 2007 3:15 am 
Newbie

Joined: Thu Mar 29, 2007 6:42 am
Posts: 10
mabraham wrote:
You are calling a static method which references sessionFactory which has never been set since the constructor is not static.

How to fix it. I'm following nhibernate reference.

_________________
Sharique uddin Ahmed Farooqui
(Asp.Net,C# developer, IT Consultant)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 15, 2007 8:12 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
I'm sorry, but no, you are not following the NHibernate reference. Go to http://www.hibernate.org/hib_docs/nhibernate/html/quickstart.html#quickstart-playingwithcats and find the differences between your code and the code there.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 16, 2007 2:04 pm 
Newbie

Joined: Thu Mar 29, 2007 6:42 am
Posts: 10
Reference willbe found at
http://www.hibernate.org/hib_docs/nhibe ... erence.pdf

_________________
Sharique uddin Ahmed Farooqui
(Asp.Net,C# developer, IT Consultant)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.