I have a VB.NET site. At the top of many of my pages I have code such as this:
Partial Class _Default
Inherits System.Web.UI.Page
Dim fns As New Functions
Dim bOnOff As Boolean
Dim LNBs As New LimsNetBusiness.SiteUI.SiteUI
Dim LNBu As New LimsNetBusiness.User.user
Dim LNBp As New LimsNetBusiness.PasswordFunctions.Password
When I publish the site. I get "Object reference not set to an instance of an object." on line:
Dim LNBs As New LimsNetBusiness.SiteUI.SiteUI
Why? And how do I fix?
You would need to show us the constructor of LimsNetBusiness.SiteUI.SiteUI I would assume.
Given that this problem only happens remotely, I'm thinking the constructor accesses an asset, connection or config file that isn't available on the server.
My recommendation is to open the DLL with Reflector and see what resources it accesses/other potentials for a null dereference.
Oddly you are saying there is no Sub New(), but I'm curious how you can create a variable of that type without having a constructor.
You mention that SiteUI passes through to your data layer - are you confident the data layer access is working fine remotely?
Not enough info. Unfortunately LimsNetBusiness is not a .net namespace. I would suggest looking into the SiteUI constructor and see if you fail inside there.
Is LimsNetBusiness a seperate DLL? Did you publish that too?
Does LimsNetBusiness.SiteUI.SiteUI reference a table, or perhaps web.config file? Maybe a table row was deleted or something similar.
This is a NullReferenceException Some where along the way a NullReferenceException is occurring.
Now you didn't provide enough information about what LimsNetBusiness is but if I had to guess:
Since I can't see you stack trace, you should be aware of the fact that the exception might be contained in the code that is instantiated in the constructor of LimsNetBusiness.SiteUI.SiteUI
If LimsNetBusiness.SiteUI is a static Property, you will need to make sure that you instantiate the returned object.
It's possible that the error is occuring in the initialization of LNBs. Since that happens in the dim statement, you probably won't see the location of the error if this is true. You can try moving the initialization of LNBs to an assignment statement:
Dim LNBs As LimsNetBusiness.SiteUI.SiteUI
LNBs = new LimsNetBusiness.SiteUI.SiteUI
Also, check in the initialization of LimsNetBusiness.SiteUI.SiteUI and make sure there is a "new" everywhere that there should be.
Related
I'm trying to create a singleton object in my ASP.NET web app. The definition is like this:
Public Module Providers
Public AppConnectionStringProvider As IConnectionStringProvider
End Module
And I'm setting this in Global.asax like this:
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
...
'create a default connection string provider
AppConnectionStringProvider = New MyConnectionStringProvider
....
End Sub
This works fine, but sometimes I see errors in my log file that are caused, ultimately, by using AppConnectionStringProvider when it is Nothing/null. I thought that by initializing it in Application_Start I would guarantee that this variable is always non-null, but there seem to be some circumstances where this is not so. What could cause this? I'm thinking of threading issues, but I can't see what they would be.
Edit: Below is the stack trace. Roughly, it's a simple request for the site home page:
Exception of type 'System.Web.HttpUnhandledException' was thrown.
System.Web.HttpApplication.ExecuteStep
System.Web.HttpApplication.CallHandlerExecutionStep
System.Web.HttpApplication.IExecutionStep.Execute
ASP.default_aspx.ProcessRequest
System.Web.UI.Page.ProcessRequest
System.Web.UI.Page.ProcessRequest
System.Web.UI.Page.ProcessRequest
System.Web.UI.Page.ProcessRequestMain
System.Web.UI.Page.HandleError
Object reference not set to an instance of an object.
System.Web.UI.Page.ProcessRequestMain
System.Web.UI.Control.LoadRecursive
MyApp.Default.Page_Load.21[Default.aspx.vb]
...
MyApp.SomeModule..ctor.6[Utilities.vb]
MyApp.get_ConnectionString.70[Connections.vb]
And in that last method, at that line of code, it's trying to use the object that was initialized in Application_Start, but it seems to be null, when it should have a value.
If your singleton object should dispose at sessions end then add your object to a session variable. Session[MyConnectionStringProvider] = New MyConnectionStringProvider(). alternatively if you'd like your singleton object to be alive for the application and share it with all users then add it to an application variable. Application[MyConnectionStringProvider] = New MyConnectionStringProvider. This way your object will not be null and available for all requests. I hope this helps.
The problem turned out to be a very subtle sequencing issue. I had a class that referenced the AppConnectionStringProvider object, but the code that set the provider object had a constructor that implicitly created an object that created another object that created another object that depended on the provider. So even though it looked like the object could not be null, the code that set it depended on it not being null. I had to refactor a bit to disentangle these dependencies, but now it's working fine. Thanks for everyone's comments.
I have dataaccass in my solution. I am using this dataaccess to retrieve data. and i am calling the function of the dataaccess part from my aspx.cs page my code sample as follows:
DataTable dt = new DataTable();
dt = objYPCategoryMasterDataAccess.GetAllSubCategory(CategoryID);
when i build it goes well but when i debug it it throws an exception as object reference not set to the instance of the object. help me please.
with Regard
This Type error occurs when your Object Is null. You Can set It By
objYPCategoryMasterDataAccess= new YPCategoryMasterDataAccess();
Try this It may help you
This error tells you that you are calling an object on a method that has no value. In your code, this probably means that objYPCategoryMasterDataAccess is null. You can set a breakpoint in your code and then debug it to inspect the value at runtime.
Recently I dealt with a commercially available ASP.NET product that shall go unnamed. When poking around in the code, I noticed that there was usercontrol casting that looked like this:
Dim ctl As ASP.modules_controls_addressinput_ascx = DirectCast(Me.FindControl("AddressInput1"), ASP.modules_controls_addressinput_ascx)
More recently I needed to cast a usercontrol to its specific type in one of my own projects so I could access its public properties and naturally I copied the casting method from above, since I had not seen another way to do it.
However, when deploying the project with this type of casting it would "Build", but failed when I tried to "Publish" with the error "Unknown Type". After some tinkering, I realized that the type of the declared class would work as follows:
Dim ctl As Modules_Controls_AddressInput = DirectCast(Me.FindControl("AddressInput1"), Modules_Controls_AddressInput)
Where the usercontrol is declared like this in its ascx.vb file:
Partial Class Modules_Controls_AddressInput
Inherits System.Web.UI.UserControl
And indeed, this also fixed the issue with publishing.
My question is what would compel someone to cast the first way vs the second way, especially when it means that publishing the project will fail?
I am not sure but the first approach will cast your control to the compiled code in asp.net temp folder C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\ProjectName but the second approach will cast it to a the class itself. In my work usually I use LoadControl("UserControlPath") to create an instance of any user control
Dim ctrl As MyControl = CType(Page.LoadControl("MyControl.ascx"), MyControl)
ctrl.Property1 = value1
ctrl.Property2 = value2
for more further information about user controls in ASP.Net you can refer to this post http://msdn.microsoft.com/en-us/library/ms972975.aspx
I have attempted to make my first 3 tier application. In the process I have run into one problem I am yet to find an optimal solution for.
Basically all my objects use an IFillable interface which forces the implementation of a sub as follows
Public Sub Fill(ByVal Datareader As Data.IDataReader) Implements IFillable.Fill
This sub then expects the Ids from the datareader will be identical to the properties of the object as such.
Me.m_StockID = Datareader.GetGuid(Datareader.GetOrdinal("StockID"))
In the end I end up with a datalayer that looks something like this.
Public Shared Function GetStockByID(ByVal ConnectionString As String, ByVal StockID As Guid) As Stock
Dim res As New Stock
Using sqlConn As New SqlConnection(ConnectionString)
sqlConn.Open()
res.Fill(StockDataLayer.GetStockByIDQuery(sqlConn, StockID))
End Using
Return res
End Function
Mostly this pattern seems to make sense. However my problem is, lets say I want to implement a property for Stock called StockBarcodeList. Under the above mentioned pattern any way I implement this property I will need to pass a connectionstring to it which obviously breaks my attempt at layer separation.
Does anyone have any suggestions on how I might be able to solve this problem or am I going about this the completely wrong way? Does anyone have any suggestions on how I might improve my implementation? Please note however I am deliberately trying to avoid using the dataset in any form.
Use the app.config file for your connection string.
Is there a particular reason you pass ConnectionString at all? It seems like a configuration value to me? So using something like a constant (or a Config singleton) might be a better idea.
What is the best way to provide strongly typed access to the session object? I am planning on turning on Option Strict, which is causing the compiler to complain about my lazy programming technique of directly accessing the session object:
Dim blah As Integer = Session("Blah")
My initial thought is to create a class that wraps the session and provides strongly typed properties for the information stored in the session. However, I cannot decide if the class should be a singleton, or instantiated on every use, or where the code should reside (i.e. within the web project or within a class library).
I'm leaning towards a singleton in my class library, but I don't know if that is the best solution, or if I am missing any other possibilities.
Proposed Solution:
Public Class SessionAccess
Public Shared Property Blah(ByVal session As HttpSessionState) As Integer
Get
Return Convert.ToInt32(session("Blah"))
End Get
Set(ByVal value As Integer)
session("Blah") = value
End Set
End Property
End Class
Code Behind:
Dim blah As Integer = SessionAccess.Blah(session)
I deleted my original answer as #Jason Berkan made a very good point when he questioned my answer. Jason, I think this idea is fine.
The only thing I would change in your code example is to check to ensure that the session variable exists.
Either my proposal is the "standard" way to do it, or else no one wraps their session access, since this question hasn't received very many answers.
I did find one line in this answer that mentioned creating a SessionManager:
Wrap the ASP.NET Session with a
SessionManager to avoid development
mistakes in spelling, etc. when
referencing items from Session.
I have not thought of any reason to not use a singleton class to provide typed access to the session, so that is the solution I went with in the project.