Object reference not set to an instance of an object

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,

kick it on DotNetKicks.com

4 Comments:

At October 21, 2007 10:05 PM, Anonymous Anonymous 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, Anonymous Anonymous 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, Blogger 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, Anonymous Rosie said...

Thanks for writing this.

 

Post a Comment

Links to this post:

Create a Link

<< Home