+ Reply to Thread
Results 1 to 2 of 2

Thread: Object reference not set to an instance of an object

  1. #1
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,533
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    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, give me rep.
    If you like it here...throw us a few bones to help support us.


  2. #2
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,382
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    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. Void where prohibited by law. Not valid in California. Your mileage may vary.

+ Reply to Thread

Similar Threads

  1. VBScript Functions Reference
    By jmurrayhead in forum ASP Development
    Replies: 25
    Last Post: October 21st, 2008, 09:31 AM
  2. Object Required?!?!?
    By jarvelous in forum ASP Development
    Replies: 2
    Last Post: March 20th, 2008, 01:46 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO