Object reference not set to an instance of an object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hrutu
    New Member
    • Dec 2013
    • 3

    Object reference not set to an instance of an object

    I am getting an error "object reference not set to an instance of an object"

    Code:
    Partial Class dynamic_array
        Inherits System.Web.UI.Page
    
        Dim s() As String
        Dim i As Integer = 0
    
        Protected Sub btn_set_Click(sender As Object, e As System.EventArgs) Handles btn_set.Click
    
            ReDim s(CInt(txt_no.Text))
        End Sub
    
        Protected Sub btn_save_Click(sender As Object, e As System.EventArgs) Handles btn_save.Click
            Dim i As Integer = 0
    
            Try
                For i = 0 To s.Length - 1
                    s(i) = InputBox("enter name " + (i + 1).ToString)
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
           
    
        End Sub
    
        Protected Sub btn_get_Click(sender As Object, e As System.EventArgs) Handles btn_get.Click
    
            Dim str As String = ""
    
            Dim i As Integer = 0
    
            Try
                For i = 0 To s.Length - 1
                    str &= s(i) + vbCrLf
                Next
    
                MsgBox(str)
    
            Catch ex As Exception
                MsgBox(ex.Message)
    
            End Try
        End Sub
    End Class
    Last edited by Frinavale; Dec 10 '13, 05:42 PM. Reason: Added code tags. Added the problem stated in the title to the body of the tread to give context the code posted.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Hi there!

    You are experiencing a NullRefereceExc eption. This exception is thrown when you attempt to use a variable that you have declared but have not instantiated it beforehand.

    For example, the following will throw a NullRefrenceExc eption:
    Code:
    Dim x As SomeObject
    x.PropertyOfSomeObject = "a value"
    The reason the exception would be thrown in this case is because I am attempting to set the PropertyOfSomeO bject property to something but have not instantiated x yet.

    The following code will NOT throw the exception because I have instantiated x before attempting to use it:
    Code:
    Dim x As SomeObject
    x = new SomeObject()
    x.PropertyOfSomeObject = "a value"
    I could have simplified that to:
    Code:
    Dim x As New SomeObject
    x.PropertyOfSomeObject = "a value"

    In your praticular case you have declared an array of string's s on line 4 and have not instantiated it before you attempt to access it on line 17.

    Consider instantiating this array in the Page Load Event.

    -Frinny

    (PS. using MsgBox in a web application will not display a message to the user accessing the web application through a web browser)
    Last edited by Frinavale; Dec 10 '13, 07:14 PM.

    Comment

    Working...