DeveloperBarn Forums

DeveloperBarn

Programming & IT forum

Object reference not set to an instance of an object

This is a discussion on Object reference not set to an instance of an object within the .Net Development forums, part of the Programming & Scripting category; This is a common error that .Net developers often face at some time or another while developing their applications. This ...

Go Back   DeveloperBarn Forums > Programming & Scripting > .Net Development

  #1  
Old May 28th, 2008, 09:41 PM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Real name: Jason
Location: Washington, D.C.
Posts: 1,953
Blog Entries: 8
Rep Power: 15
jmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud of
Default Object reference not set to an instance of an object

This is a common error that .Net developers often face at some time or another while developing their applications. This is to be used as a guide for diagnosing the common causes of this error.

Prior to .Net 2.0, you had to declare every control in your code behind. For example:

.aspx
Code:
<asp:TextBox ID="FirstName" runat="server" />
.aspx.vb
Code:
Public Class MyPage
Inherits System.Web.UI.Page
 
Protected WithEvents FirstName As System.Web.UI.WebControls.TextBox 
Without the bolded line above, you would receive the error mentioned in the title of this thread. This is ONLY in versions of .Net prior to 2.0.

Variable scoping is another common issue. For example, you can't declare a variable in one sub and then expect it to work in another:

Code:
Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Dim myVariable As String = "Test"
End Sub
 
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Response.Write(myVariable)
End Sub
The above will result in the error because the variable "myVariable" is out of scope in the Button1_Click sub.

Bad inits and constructs are another cause. Take the following example:

Code:
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) 
     Dim dirInfo As System.IO.DirectoryInfo
     dirInfo.GetDirectory("C:\")
End Sub
Above, we have defined dirInfo as a DirectoryInfo object but haven't actually created it:

Code:
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Dim dirInfo As System.IO.DirectoryInfo
    dirInfo = New System.IO.DirectoryInfo("C:\")
End Sub
Another possible cause of this error is if you do not give a user control an ID:

Code:
<YourPrefix:YourControl runat="server" />
The correct way:
Code:
<YourPrefix:YourControl ID="myControlID" runat="server" />
If anyone has any other causes, feel free to add them here.
__________________
jmurrayhead
If you agree with me... click the icon!
If my post solved your problem, click the button in the lower right-hand corner of the post.

If you like it here...throw us a few bones to help
support us.

Join our Folding team: DeveloperBarn Folding

Reply With Quote
  #2  
Old May 29th, 2008, 10:16 AM
Wolffy's Avatar
Wolfmaster
 
Join Date: Mar 2008
Real name: Wolff
Location: Peoria, IL
Posts: 779
Blog Entries: 1
Rep Power: 9
Wolffy is a splendid one to beholdWolffy is a splendid one to beholdWolffy is a splendid one to beholdWolffy is a splendid one to beholdWolffy is a splendid one to beholdWolffy is a splendid one to beholdWolffy is a splendid one to behold
Default

Another possible way to cause this error is not check for null or Nothing prior to using an object. For example:
Code:
TextBox tb = gvRow.FindControl("nonesuch") as TextBox;
tb.Text;
If "nonsuch" does not exists (as if I coded ID="NoneSuch" in my apsx page)
__________________
Wolffy
------------------------
Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Rework for your specific environment may be required. Void where prohibited by law. Not valid in California. Your mileage may vary.
Reply With Quote
Reply

  DeveloperBarn Forums > Programming & Scripting > .Net Development

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
VBScript Functions Reference jmurrayhead ASP Development 25 October 21st, 2008 08:31 AM
[Error] Object Required?!?!? jarvelous ASP Development 2 March 20th, 2008 12:46 PM


All times are GMT -4. The time now is 10:22 PM.


Copyright ©2008-2010, DeveloperBarn

Content Relevant URLs by vBSEO 3.3.2