object reference not set to an instance of object
I have a webapplication that only for saving a uploadded video file's path to the sqlserver database.i have a bussiness logicslayer in the application.
I am using asp.net/vb.net. I put this in the bll for connecting with stored procedure,
Dim Db As Database = DatabaseFactory.CreateDatabase()
there is a error that--
开发者_StackOverflow中文版object reference not set to an instance of an object
if any one knows please help me.
Here is the stack trace:
[NullReferenceException: Object reference not set to an instance of an object.]
Microsoft.Practices.EnterpriseLibrary.Data.DatabaseConfigurationView.get_DefaultName() +38
Microsoft.Practices.EnterpriseLibrary.Data.DatabaseMapper.MapName(String name, IConfigurationSource configSource) +65
Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfigurationNameMappingStrategy.BuildUp(IBuilderContext context, Type t, Object existing, String id) +122
Microsoft.Practices.ObjectBuilder.BuilderBase`1.DoBuildUp(IReadWriteLocator locator, Type typeToBuild, String idToBuild, Object existing, PolicyList[] transientPolicies) +316
Microsoft.Practices.ObjectBuilder.BuilderBase`1.BuildUp(IReadWriteLocator locator, Type typeToBuild, String idToBuild, Object existing, PolicyList[] transientPolicies) +168
Microsoft.Practices.ObjectBuilder.BuilderBase`1.BuildUp(IReadWriteLocator locator, String idToBuild, Object existing, PolicyList[] transientPolicies) +104
Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory.BuildUp(IReadWriteLocator locator, IConfigurationSource configurationSource) +228
Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory.BuildUp(IConfigurationSource configurationSource) +65
Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.NameTypeFactoryBase`1.CreateDefault() +72
Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase() +86
Uploader.BllUpdate.SaveFileDetails() in D:\Developers\Sinoy\uploader\Uploader\Uploader\BLL\BllUpdate.vb:37
Uploader.Upload.UploadBtn_Click(Object sender, EventArgs e) in D:\Developers\Sinoy\uploader\Uploader\Uploader\Forms\Upload.aspx.vb:17
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102
object reference not set to an instance of object means that there is a reference which wasnt instantiated before it was used. The reference is still null (or in VB lingo, Nothing).
Catch the exception and check the stack trace on where it occured.
Usually when you get that error it is because an class variable (such as DatabaseFactory or a variable inside that class) is used before it has been assigned an instance of the class, as with the New keyword in the Dim statement or assignment. There are other ways to assign instances to a variable, too. If you don't, you'll get this error message.
The call into the Microsoft Enterprise Library static method DatabaseFactory.CreateDatabase is causing something inside the Microsoft Enterprise Library to crash - it's using a variable that has no value (it is Nothing
). For some reason it is unable to connect to your database. Why it doesn't throw a more informative exception I don't know - you'd have to ask Microsoft.
I know nothing about the Enterprise Library, but I think I'd check your web.config or app.config configuration settings looking at the database connection strings for the default database. This is based on a 5 second skim-read of the documentation and some Googling of online forums.
First, make sure you set the default database, i.e.
Also, if it is a virtual directory, make sure your Web.config file inside that virtual directory does not get overridden by the root directoriy's Web.config EnterpriseLibrary related elements.
Did you check your web.config? Have you set the Database connection string properly? This issue also happens when your connection string is missing or not proper.
精彩评论