VB.NET: Object reference not set to an instance of an object
September 22, 2007
I recently struggled with a nasty runtime error.
Ever see this?
NullReferenceException – Object reference not set to an instance of an object
NullReferenceException was unhandled. Object reference not set to an instance of an object.
Tips: use the “new” keyword to create an object instance.
Dim thing as New Thing
thing = Nothing
'do something at start of load event
if thing = "sandwich" Then
thing = "jelly"
Clearly the thing object IS set to an instance of on object at this point. It’s an instance of the Thing class. I even made sure to initialize the private thing object after reading on one of Microsoft’s pages that initializing the obj should help in this case. Apparently it doesn’t.
After scratching my head over this for a while, I came across a few forums that suggested this NullReferenceException error was an actual Bug in Visual Studio 2005.
Go figure.
I’ve since found ways to work around the problem – only to discover that once portions of the code that handle the work around, I can go back to assigning the value as I showed above.
Crazy stuff!

September 27, 2007 at 1:54 am
Hi, first of all your classes need constructors. Inside the constructors pass the objects ByRef and assign them to private variables inside the class. Then when you instantiate a new copy of the class pass the objects you wish to modify to the constructor. Now the passed objects can be accessed directly and you avoid the NullReferenceException issues altogether.
October 30, 2007 at 8:41 pm
I have spent an aggregate of days and days chasing this issue around. I appreciate your taking the time to post your solution. I think Nulflux might have a good point if I could get him to translate it. Just when I think I am somewhat knowledgeable, I read a paragraph like his and feel like I am back in 1st grade reading college level texts.
I am using a Gridview with an ODS. I get the error on the “If” line.
Protected Sub gvItemz_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)Handles gvItemz.RowDataBound
Dim cbS As CheckBox = CType(e.Row.FindControl(“tbStnd”), CheckBox)
If cbS.Checked =True Then
Dim tbQ As TextBox = CType(e.Row.FindControl(“tbQnty”), TextBox)
tbQ.Visible = False
End If
Any ideas????????? Please!!!!!!!!!!!!!!!!!!
Thanks!
October 30, 2007 at 9:00 pm
Steve,
I also feel a tad dumb. I don’t know what an ODS is. …wikipedia suggests “Operation Desert Storm” but that can’t be it.
ah, this must be it: operational data store ( a type of database…)
ok, that make sense and is probably not relevant to the problem.
am I understanding that the checkbox is on the gridRow? (in a cell?) or not?
does “if cbS.Checked then” do anything different?
if not, it can’t be a problem with the condition checking.
it has to be related to the :
“Dim cbS As CheckBox = CType(e.Row.FindControl(”tbStnd”), CheckBox)”
which I’m not entirely clear about what you are doing. can you explain that bit?
November 1, 2007 at 1:15 pm
Hmm… Yes VB.net does have class constructors however it is rarely mentioned. Here is an example of what I mean:
Lets say you have 2 classes in your project – a form class (for the GUI) and another class (perhaps a network wrapper). The form class allows the user to configure the options for the network wrapper. The first problem you might encounter is that you can’t pass variables from the form class to the network wrapper class because of the NullReferenceException.
To solve this issue you would simply allow the network wrapper to take the GUIForm as input to it’s constructor:
Class GUIForm
‘Here is a variable we need to pass to the NetworkWrapper
Public CurrentUser as String
‘——GUI Form class code goes here—-’
End Class
Class NetworkWrapper
‘This is our local pointer to the GUIForm
Dim GUIFormReference as GUIForm
‘Pass the GUIForm to the NetworkWrapper constructor.
Private Sub New(ByRef ObjectReference as GUIForm)
GUIFormReference = ObjectReference
End Sub
‘Now we can access GUIForms variables like so:
Private Sub DoSomethingToGUIForm()
Dim cu as String = GUIFormReference.CurrentUser
‘Likewise we could set the value
GUIFormReference.CurrentUser = “ME!!!”
End Sub
End Class
I hope I got that right it’s 8:15am here and I haven’t slept yet ; )
November 1, 2007 at 1:18 pm
Oh yes I did forget something – here’s revised GUIForm class code:
Class GUIForm
‘Here is a variable we need to pass to the NetworkWrapper
Public CurrentUser as String
‘This is what I forgot – a new instance of the NetworkWrapper class passing the GUIForm to the constructor:
Dim n as new NetworkWrapper(Me)
‘——GUI Form class code goes here—-’
End Class
Sorry, like I said it’s late… erm… early…
November 1, 2007 at 1:27 pm
I realize this example is a bit off-topic to your specific issue however I think the root problem is similar – I’ll check back in a few days to see if this helped.
November 1, 2007 at 11:56 pm
Nulflux,
I see what you mean. I’ve used that method before and I agree, it certainly works.
I’m not sure, but I’ve heard that VS2008 has fixed this problem – … I’m relatively certain that what I was originally doing *should* have worked… but the work-around isn’t that big of a hassle.
Thanks for clarifying your original point
March 5, 2008 at 6:43 am
Hi All,
I have a project in VS.Net 2003 (VB.Net). The OS is Vista Ultimate.
The project runs fine without any problem in Vista Ultimate and XP.
I create a setup for my project in Vista it works, but i get a error in a button_click “Object reference not set to an instance of an object.” in one form which calls dll.
This error comes only in Vista in a button click, i am not getting it in XP. Thanks for any support.
Thanks,
V.Chock
May 20, 2008 at 12:42 pm
The error “Object reference not set to an instance of an object”
Keeps throwing!
When I connect to Sql 2000 Database. I am not getting such an error. While, connectin gto SQL 2005 Databse, then Iam getting the error “Object reference not set to an instance of an object”
Could you give solution!
Thanks
Guru
May 27, 2008 at 1:11 pm
This is my code & im geting same error
———–XXXX———–
Dim dr As DataRowView = e.ListItem.DataItem()
‘Dim lblPropDetailsname As Label = CType(e.ListItem.FindControl(“lblPropDetailsname”), Label)
‘Dim lblPropDetails As LinkButton = CType(e.ListItem.FindControl(“lblPropDetails”), LinkButton)
Dim lblPropDetailsname As New Label
Dim lblPropDetails As New LinkButton
lblPropDetailsname = CType(e.ListItem.FindControl(“lblPropDetailsname”), Label)
lblPropDetails = CType(e.ListItem.FindControl(“lblPropDetails”), LinkButton)
If dr(“Geograph_id”) = “A1″ And dr(“Geograph_Prop_Id”) = “1″ Then
lblPropDetailsname.Text = dr(“Geograph_name”)
lblPropDetails.Visible = False
End If
If dr(“Geograph_id”) = “A1″ Then
lblPropDetails.Text = dr(“name”)
End If
———–XXXX————-
Can you please Help me geting solved the same error, as i tried using “New” also
October 12, 2008 at 5:35 pm
hello
I received this error:
Object reference not set to an instance of an object
please help me
http://www.bitasoft.ir
پروژه های دانشجویی
December 17, 2008 at 12:04 pm
DEAR FRIEND
I HAVE ONE BIGEST PROBLEM IN VB.NET
ADO DON NET. ERROR
“Object reference not set to an instance of an object”
PLEASE SLOW THE MY ERROR
BEST REGARDS
MAHENDRA RANE
9099121384
January 1, 2009 at 1:32 am
hmsabuaugxwuidbowell, hi admin adn people nice forum indeed. how’s life? hope it’s introduce branch
July 14, 2009 at 1:29 pm
I am trying to query the database on every change of the selected index of a listview control.
A message pops up saying Object reference not set to instance of an object.
July 14, 2009 at 1:30 pm
I am trying to query the database on every change of the selected index of a listview control.
A message pops up saying Object reference not set to instance of an object.
I am using VB.NET 2008 and an Ms Access Database.
August 12, 2009 at 6:01 pm
HI, I have a problem for object reference. Try to resolve this error in above method. But, i can’t resolve that error. Pls, verify my code..
ReDim Letter(0 To rsChar.RecordCount)
October 8, 2009 at 8:46 am
Dim tandaTerima As frmTandaTerimaPenagihan
Dim IdSupp As String = sqlRead.Item (“No_Sup”).ToString
If Convert.ToString(IdSupp) = Val(tandaTerima.txtIdSupplierTT.Text) Then
tu eror
kenapa y?