Spring.NET - object reference not set to object instance
I am new to .NET. I've used Spring Framework and Hibernate before in Java, but this is my first in .NET. For developing my application, I am using Spring.Northwind solution as an example.
Sleep mode display:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="EMSApplication.Domain.Employee, EMSApplication.Domain" table="ems_Employees" proxy="EMSApplication.Domain.IEmployee, EMSApplication.Domain" lazy="true">
<id name="Id" type="System.Int32">
<column name="Id" not-null="true"/>
<generator class="native"/>
</id>
<property name="Username" type="System.String">
<column name="Username" not-null="true"/>
</property>
<property name="Firstname" type="System.String">
<column name="Firstname" not-null="true"/>
</property>
</class>
</hibernate-mapping>
As you can see, the Employee class only has three fields. It implements IEmployee, which consists of defining the get and set methods of these fields (if you want to see these classes I can provide).
Now I have a Web.config. I installed Spring as a sectionGroup and defined it as:
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web"/>
<section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core"/>
</sectionGroup>
<section name="databaseSettings" type="System.Configuration.NameValueSectionHandler"/>
</configSections>
<spring>
<parsers/>
<context>
<resource uri="~/Web.xml"/>
</context>
</spring>
<databaseSettings>
<add key="db.datasource" value="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\JYM.mdf;Integrated Security=True;User Instance=True"/>
</databaseSettings>
I also added a key Spring.Data.NHibernate.Support.OpenSessionInViewModule.SessionFactoryObjectName
to appsettings:
<appSettings>
<add key="Spring.Data.NHibernate.Support.OpenSessionInViewModule.SessionFactoryObjectName" value="NHibernateSessionFactory"/>
</appSettings>
httpModules system.web has the following entries:
<httpModules>
<add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web"/>
<add name="OpenSessionInView" type="Spring.Data.NHibernate.Support.OpenSessionInViewModule, Spring.Data.NHibernate32"/>
</httpModules>
Finally, the Spring Web.xml resource has the following entities:
<object type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core">
<property name="ConfigSections" value="databaseSettings"/>
</object>
<db:provider id="DbProvider" provider="System.Data.SqlClient" connectionString="Data Source=|DataDirectory|\JYM.mdf"/>
<object id="NHibernateSessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate32">
<property name="DbProvider" ref="DbProvider"/>
<property name="MappingResources">
<list>
<value>assembly://EMSApplication/Domain/EMSApplication.hbm.xml</value>
</list>
</property>
<property name="HibernateProperties">
<dictionary>
<entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<entry key="dialect" value="NHibernate.Dialect.MsSql2008Dialect"/>
<entry key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
<entry key="proxyfactory.factory_class" value="NHibernate.Bytecode.DefaultProxyFactoryFactory, NHibernate"/>
<entry key="show_sql" value="true"/>
<entry key="hbm2ddl.auto" value="update"/>
<entry key="cache.use_query_cache" value="true"/>
</dictionary>
</property>
<property name="ExposeTransactionAwareSessionFactory" value="true" />
</object>
<object id="transactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate32">
<property name="DbProvider" ref="DbProvider"/>
<property name="SessionFactory" ref="NHibernateSessionFactory"/>
</object>
<object type="Spring.Dao.Attributes.PersistenceExceptionTranslationPostProcessor, Spring.Data"/>
<object id="EmployeeDao" type="EMSApplication.NHibernate.HibernateEmployeeDao">
<property name="SessionFactory" ref="NHibernateSessionFactory"/>
</object>
<tx:attribute-driven transaction-manager="transactionManager"/>
Now when I start debugging from Visual Studio 2010 I get
System.NullReferenceException: Object reference not set to object instance.
Full stack trace:
[NullReferenceException: Object reference not set to an instance of an object.]
System.Xml.XmlReader.CalcBufferSize(Stream input) +14
System.Xml.XmlTextReaderImpl.InitStreamInput(Uri baseUri, String baseUriStr, Stream stream, Byte[] bytes, Int32 byteCount, Encoding encoding) +66
System.Xml.XmlTextReaderImpl..ctor(String url, Stream input, XmlNameTable nt) +93
System.Xml.XmlTextReader..ctor(Stream input) +74
NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:631
[MappingException: Could not configure datastore from input stream ]
NHibernate.Cfg.Configuration.LogAndThrow(Exception exception) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:342
NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:641
NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:611
Spring.Data.NHibernate.LocalSessionFactoryObject.AfterPropertiesSet() in c:\_prj\spring-net\trunk\src\Spring\Spring.Data.NHibernate20\Data\NHibernate\LocalSessionFactoryObject.cs:599
Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.InvokeInitMethods(Object target, String name, IConfigurableObjectDefinition definition) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractAutowireCapableObjectFactory.cs:1294
Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.ConfigureObject(String name, RootObjectDefinition definition, IObjectWrapper wrapper) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractAutowireCapableObjectFactory.cs:1890
Spring.Objects.Factory.Support.WebObjectFactory.ConfigureObject(String name, RootObjectDefinition definition, IObjectWrapper wrapper) in c:\_prj\spring-net\trunk\src\Spring\Spring.Web\Objects\Factory\Support\WebObjectFactory.cs:450
Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.InstantiateObject(String name, RootObjectDefinition definition, Object[] arguments, Boolean allowEagerCaching, Boolean suppressConfigure) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractAutowireCapableObjectFactory.cs:921
[ObjectCreationException: Error creating object with name 'NHibernateSessionFactory' defined in 'file [D:\Code\EMSApplication\EMSApplication\Web.xml] line 14' : Initialization of object failed : Could not configure datastore from input stream ]
Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.InstantiateObject(String name, RootObjectDefinition definition, Object[] arguments, Boolean allowEagerCaching, Boolean suppressConfigure) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractAutowireCapableObjectFactory.cs:938
Spring.Objects.Factory.Support.AbstractObjectFactory.CreateAndCacheSingletonInstance(String objectName, RootObjectDefinition objectDefinition, Object[] arguments) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractObjectFactory.cs:2144
Spring.Objects.Factory.Support.WebObjectFactory.CreateAndCacheSingletonInstance(String objectName, RootObjectDefinition objectDefinition, Object[] arguments) in c:\_prj\spring-net\trunk\src\Spring\Spring.Web\Objects\Factory\Support\WebObjectFactory.cs:299
Spring.Objects.Factory.Support.AbstractObjectFactory.GetObjectInternal(String name, Type requiredType, Object[] arguments, Boolean suppressConfigure) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractObjectFactory.cs:2065
Spring.Objects.Factory.Support.AbstractObjectFactory.GetObject(String name) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractObjectFactory.cs:1826
Spring.Objects.Factory.Support.DefaultListableObjectFactory.GetObjectsOfType(Type type, Boolean includePrototypes, Boolean includeFactoryObjects) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\DefaultListableObjectFactory.cs:770
Spring.Objects.Factory.ObjectFactoryUtils.ObjectsOfTypeIncludingAncestors(IListableObjectFactory factory, Type type, Boolean includePrototypes, Boolean includeFactoryObjects) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\ObjectFactoryUtils.cs:268
Spring.Dao.Support.PersistenceExceptionTranslationInterceptor.DetectPersistenceExceptionTranslators(IListableObjectFactory objectFactory) in c:\_prj\spring-net\trunk\src\Spring\Spring.Data\Dao\Support\PersistenceExceptionTranslationInterceptor.cs:155
Spring.Dao.Support.PersistenceExceptionTranslationInterceptor..ctor(IListableObjectFactory objectFactory) in c:\_prj\spring-net\trunk\src\Spring\Spring.Data\Dao\Support\PersistenceExceptionTranslationInterceptor.cs:73
Spring.Dao.Attributes.PersistenceExceptionTranslationAdvisor..ctor(IListableObjectFactory objectFactory, Type repositoryAttributeType) in c:\_prj\spring-net\trunk\src\Spring\Spring.Data\Dao\Attributes\PersistenceExceptionTranslationAdvisor.cs:63
Spring.Dao.Attributes.PersistenceExceptionTranslationPostProcessor.set_ObjectFactory(IObjectFactory value) in c:\_prj\spring-net\trunk\src\Spring\Spring.Data\Dao\Attributes\PersistenceExceptionTranslationPostProcessor.cs:95
Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.ConfigureObject(String name, RootObjectDefinition definition, IObjectWrapper wrapper) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractAutowireCapableObjectFactory.cs:1886
Spring.Objects.Factory.Support.WebObjectFactory.ConfigureObject(String name, RootObjectDefinition definition, IObjectWrapper wrapper) in c:\_prj\spring-net\trunk\src\Spring\Spring.Web\Objects\Factory\Support\WebObjectFactory.cs:450
Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.InstantiateObject(String name, RootObjectDefinition definition, Object[] arguments, Boolean allowEagerCaching, Boolean suppressConfigure) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractAutowireCapableObjectFactory.cs:930
Spring.Objects.Factory.Support.AbstractObjectFactory.CreateAndCacheSingletonInstance(String objectName, RootObjectDefinition objectDefinition, Object[] arguments) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractObjectFactory.cs:2144
Spring.Objects.Factory.Support.WebObjectFactory.CreateAndCacheSingletonInstance(String objectName, RootObjectDefinition objectDefinition, Object[] arguments) in c:\_prj\spring-net\trunk\src\Spring\Spring.Web\Objects\Factory\Support\WebObjectFactory.cs:299
Spring.Objects.Factory.Support.AbstractObjectFactory.GetObjectInternal(String name, Type requiredType, Object[] arguments, Boolean suppressConfigure) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractObjectFactory.cs:2065
Spring.Objects.Factory.Support.AbstractObjectFactory.GetObject(String name) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractObjectFactory.cs:1826
Spring.Objects.Factory.Support.DefaultListableObjectFactory.GetObjectsOfType(Type type, Boolean includePrototypes, Boolean includeFactoryObjects) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\DefaultListableObjectFactory.cs:770
Spring.Context.Support.AbstractApplicationContext.GetObjectsOfType(Type type, Boolean includePrototypes, Boolean includeFactoryObjects) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Context\Support\AbstractApplicationContext.cs:1383
Spring.Context.Support.AbstractApplicationContext.RegisterObjectPostProcessors(IConfigurableListableObjectFactory objectFactory) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Context\Support\AbstractApplicationContext.cs:652
Spring.Context.Support.AbstractApplicationContext.Refresh() in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Context\Support\AbstractApplicationContext.cs:1000
Spring.Context.Support.WebApplicationContext..ctor(WebApplicationContextArgs args) in c:\_prj\spring-net\trunk\src\Spring\Spring.Web\Context\Support\WebApplicationContext.cs:125
Spring.Context.Support.WebApplicationContext..ctor(String name, Boolean caseSensitive, String[] configurationLocations) in c:\_prj\spring-net\trunk\src\Spring\Spring.Web\Context\Support\WebApplicationContext.cs:82
_dynamic_Spring.Context.Support.WebApplicationContext..ctor(Object[] ) in c:\_prj\spring-net\trunk\src\Spring\Spring.Web\Caching\AspNetCache.cs:126
Spring.Reflection.Dynamic.SafeConstructor.Invoke(Object[] arguments) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Reflection\Dynamic\DynamicConstructor.cs:116
Spring.Context.Support.RootContextInstantiator.InvokeContextConstructor(ConstructorInfo ctor) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Context\Support\ContextHandler.cs:563
Spring.Context.Support.ContextInstantiator.InstantiateContext() in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Context\Support\ContextHandler.cs:508
Spring.Context.Support.ContextHandler.InstantiateContext(IApplicationContext parentContext, Object configContext, String contextName, Type contextType, Boolean caseSensitive, String[] resources) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Context\Support\ContextHandler.cs:351
Spring.Context.Support.WebContextHandler.InstantiateContext(IApplicationContext parent, Object configContext, String contextName, Type contextType, Boolean caseSensitive, String[] resources) in c:\_prj\spring-net\trunk\src\Spring\Spring.Web\Context\Support\WebContextHandler.cs:127
Spring.Context.Support.ContextHandler.Create(Object parent, Object configContext, XmlNode section) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Context\Support\ContextHandler.cs:289
[ConfigurationErrorsException: Error creating context 'spring.root': Could not configure datastore from input stream ]
System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult) +199
System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject) +1153
System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject) +1468
System.Configuration.BaseConfigurationRecord.GetSection(String configKey) +41
System.Web.HttpContext.GetSection(String sectionName) +52
System.Web.Configuration.HttpConfigurationSystem.GetSection(String sectionName) +57
System.Web.Configuration.HttpConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String configKey) +6
System.Configuration.ConfigurationManager.GetSection(String sectionName) +78
Spring.Util.ConfigurationUtils.GetSection(String sectionName) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Util\ConfigurationUtils.cs:71
Spring.Context.Support.WebApplicationContext.GetContextInternal(String virtualPath) in c:\_prj\spring-net\trunk\src\Spring\Spring.Web\Context\Support\WebApplicationContext.cs:335
Spring.Context.Support.WebApplicationContext.GetRootContext() in c:\_prj\spring-net\trunk\src\Spring\Spring.Web\Context\Support\WebApplicationContext.cs:223
Spring.Context.Support.WebSupportModule.Init(HttpApplication app) in c:\_prj\spring-net\trunk\src\Spring\Spring.Web\Context\Support\WebSupportModule.cs:175
System.Web.HttpApplication.InitModulesCommon() +172
System.Web.HttpApplication.InitModules() +43
System.Web.HttpApplication.InitInternal(HttpContext context, HttpApplicationState state, MethodInfo[] handlers) +828
System.Web.HttpApplicationFactory.GetNormalApplicationInstance(HttpContext context) +304
System.Web.HttpApplicationFactory.GetApplicationInstance(HttpContext context) +107
System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr) +327
Here you can see:
[ObjectCreationException: Failed to create an object named "NHibernateSessionFactory" defined in file [D: \ Code \ EMSApplication \ EMSApplication \ Web.xml] line 14 ': Object initialization failed: Could not configure datastore from input stream]
So I cannot figure out how to solve this problem. Any help or suggestion that I really appreciate.
Thank.
NB: I haven't changed any settings in IIS 7.
Edit:
These are the links used:
source to share
Is it an assembly://EMSApplication/Domain/EMSApplication.hbm.xml
embedded resource (VS -> Properties -> Build Actions -> Embedded Resource)?
However, instead of pointing to an embedded resource, I would suggest pointing to an assembly, as @Marijn already pointed out in a previous post:
<propertyname="MappingAssemblies">
<list>
<value>EMSApplication.Domain</value> <-- Assembly name
</list>
</property
source to share