-2

asp.net (with vb.net) control array at run time giving error :

Object reference not set to an instance of an object.

I am getting this error.

My Code is :

Partial Class _Default
Inherits System.Web.UI.Page
Dim chk(10) As CheckBox

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack Then
Else
MsgBox("loading")
chk(0) = New CheckBox
With chk(0)
 .ID = "chk(0)"
 .Text = .ID
End With
Me.form1.Controls.Add(chk(0))
End If
TextBox1.Text = chk(0).Text
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If chk(0).Checked = True Then
MsgBox("Yes")
Else
MsgBox("No")
End If
Response.Redirect("Page1.aspx")
End Sub
End Class
Raviteja
  • 3,399
  • 23
  • 42
  • 69
  • 2
    Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Rahul Singh Jan 19 '16 at 06:50

2 Answers2

0

Use this instead:

 Dim chk As CheckBox 
    chk = New CheckBox 
CodeConstruct
  • 290
  • 3
  • 17
0

Solved !

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack Then
Else
    MsgBox("loading")
    chk(0) = New CheckBox
    With chk(0)
         .ID = "chk(0)"
         .Text = .ID
    End With
    Me.form1.Controls.Add(chk(0))
End If
TextBox1.Text = chk(0).Text

End Sub