locked
Help with null exception reference RRS feed

  • Question

  • User256767925 posted

     When the debugging it will stop at str = str & t.Text with the error null exception reference

    Can someone tell me why is it happening?

    Thx

    Partial Class _Default
        Inherits System.Web.UI.Page
    
        Public Sub AddTextBox()
    
            Dim n As Integer
            Dim i As Integer
    
            Dim txt As TextBox
    
            n = Trim(txt_quant.Text)
    
            For i = 1 To n
    
                txt = New TextBox
                txt.ID = "txt_sn" & i
                PlaceHolder1.Controls.Add(txt)
    
            Next
    
        End Sub
    
        Protected Sub btn_ok_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_ok.Click
    
            AddTextBox()
    
        End Sub
    
        Protected Sub btn_submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_submit.Click
    
            Dim n As Integer
            Dim str As String = ""
    
            Dim t As New TextBox
    
            n = Trim(txt_quant.Text)
    
    
            For i = 1 To n
    
                t = PlaceHolder1.FindControl("txt_sn" & i)
                str = str & t.Text
    
            Next
    
            Response.Write(str)
    
        End Sub
    End Class


     

    Thursday, July 16, 2009 8:57 PM

Answers

  • User187056398 posted

    FindControl is not finding a control so it is returning 'nothing' (null).

    You do assign a new TextBox to t (nice variable name by the way) but then overwrite it with the results from FindControl.

     

     

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 16, 2009 9:56 PM