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 ...
| |||||||
|
#1
| ||||
| ||||
| 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" /> Code: Public Class MyPage Inherits System.Web.UI.Page Protected WithEvents FirstName As System.Web.UI.WebControls.TextBox 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
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
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
Code: <YourPrefix:YourControl runat="server" /> Code: <YourPrefix:YourControl ID="myControlID" runat="server" />
__________________ 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 |
|
#2
| ||||
| ||||
| 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;
__________________ 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. |
![]() |
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
| |
| ||||
| 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 |