Object reference not set to an instance of an object Wednesday, February 14, 2007
Object reference not set to an instance of an object.
In Asp.Net this error is so common.
When you are trying to access a member of a class without instance object you will get Object reference not set to an instance of an object error.
So ensure that you are accessing the members with the instance of the class.
Example:
Cause of error:
public class ABC{
public void function xyz{....}
}
public class DEF{
ABC obj1;
obj1.xyz();
}
The highlighted line indicates that you have declared a variable of type ABC, but not the object reference or you just have created Object instance.
We can call methods or member variables only if we get reference to the instance of a object.
More technically,
ABC obj1; will not create any reference in memory.
Solution:
After the line ABC obj1 or in the same line itself ,
obj1=new ABC();
the keyword new will allot memory for the instance obj1 and assign the object reference to the instance of the object.
Other lines would be normal and kept unchanged.
Unusual:
Even after this if you are getting the same error Object reference not set to an instance of an object, it means that you have to initialize some member variables.
Example:
string example;
if(example=="answer")
.......
Obviously this error would come.
since we failed to initialize this variable no reference would have have got.
So before checking any variable comparison, kindly make ensure to add these lines
if(example!=null)
if(example=="answer")
.......
So that you can avoid comparing a null object(for which reference not been set) as well as the
above said error Object reference not set to an instance of an object.
Same discussion is available in Object reference not set to an instance of an object.
posted by TechieLion @ 9:19 PM,
![]()
12 Comments:
- At October 21, 2007 10:05 PM, said...
-
But this will not work for SQLDataReader. I cannot do a New initialization on SQLDataReader. The message says it does not have a constructor. My class with SQLDataReader will still error out on this although other functions work fine. Any clues?
- At January 27, 2008 12:25 PM, said...
-
If k = CType(CType(sender, DevExpress.XtraReports.UI.XRLabel).Text, Integer) Then
pageno += 1
Else
pageno = 1
End If
CurID = CType(sender, XRLabel).Text.ToString()
Dim PageCount As Integer = 0
Dim Page As Page
For Each Page In Me.PrintingSystem.Document.Pages
Dim BrickEnum As BrickEnumerator = Page.GetEnumerator()
While BrickEnum.MoveNext()
Dim Brick As XRBrick = BrickEnum.Current
If Not (Brick Is Nothing) And Brick.Data.Control Is CType(objSender, XRLabel) Then
If Brick.Data.Text.ToString() = CurID Then
PageCount += 1
End If
End If
End While
Next Page
'If CurID = LastID Then
' PageNumber += 1
'Else
' PageNumber = 1
'End If
'LastID = CurID
'CType(sender, XRLabel).Text = "Page " + PageNumber.ToString() + " of " + PageCount.ToString()
k = CType(CType(sender, DevExpress.XtraReports.UI.XRLabel).Text, Integer)
CType(sender, DevExpress.XtraReports.UI.XRLabel).Text = "Page:" & pageno & " of " & PageCount
CType(objSender, XRLabel).Visible = False - At April 25, 2008 5:36 AM, Shalendra said...
-
I tried this code but it does not work properly.
Function Branch()
Dim obj As abcl5.Classes.ClsOperations
Dim ds As New DataSet
ds = obj.getbranch()
Dim lst1 As New ListItem
Dpdbranchcode.Items.Clear()
lst1.Value = "Select"
Dpdbranchcode.Items.Add(lst1)
Dim j As Integer
Dim k As Integer
j = ds.Tables(0).Rows.Count
For k = 0 To j - 1
Dim lst As New ListItem
lst.Value = ds.Tables(0).Rows(k).ItemArray(0).ToString()
lst.Text = ds.Tables(0).Rows(k).ItemArray(1).ToString()
Dpdbranchcode.Items.Add(lst)
Next
Dpdbranchcode.DataBind()
End Function - At November 11, 2008 1:57 AM, Rosie said...
-
Thanks for writing this.
- At December 21, 2009 5:05 PM, said...
-
chains prohibit incapacity maintains osyrup sars rebuttal gazette assuring revisit doubtful
semelokertes marchimundui - At March 6, 2010 6:02 AM, said...
-
It not a joke!
- At March 10, 2010 2:54 AM, said...
-
You have really great taste on catch article titles, even when you are not interested in this topic you push to read it
- At March 13, 2010 12:56 AM, said...
-
I would appreciate more visual materials, to make your blog more attractive, but your writing style really compensates it. But there is always place for improvement
- At March 13, 2010 10:15 PM, said...
-
It absolutely not agree with the previous phrase
- At December 3, 2010 5:52 PM, free young taboo sex stories said...
-
He remembered Stephanies birthday. It wasfunny seeing John try to put on a bra, especially with real breasts thistime.
free porn pics an stories
femdom wife spanking stories
free erotic and sex stories
erotic sex stories about little girls
reverse prayer bondage stories
He remembered Stephanies birthday. It wasfunny seeing John try to put on a bra, especially with real breasts thistime. - At December 4, 2010 4:29 AM, free erotic stories porn said...
-
As she went throughher toiletries a devious plan began to formulate inher mind and added an extra spark and spring in herstep. Iset my camera up to tape a little of it and I can see my wife is beingsmothered by my bosss girlfriend on one side and the black guy on theother.
erotic lactation stories
free stories adult
xxx porn stories
sister incest stories
stories first time cock suckers
As she went throughher toiletries a devious plan began to formulate inher mind and added an extra spark and spring in herstep. Iset my camera up to tape a little of it and I can see my wife is beingsmothered by my bosss girlfriend on one side and the black guy on theother. - At February 24, 2012 5:04 AM, said...
-
If you always write interesting, I will be your regular reader. skin care Read a useful article about tramadol tramadol



