VB.NET - Null Reference Exception was unhandled

I have a set of Checkboxes each with a Textbox next it to. I have the following code, which is meant to loop through each CheckBoxe and if checked if gets the text contained in the textbox, these are linked through the tag of the checkbox, i have used it before and it worked fine but this time i am getting the following error:

Null Reference Exception was unhandled
Object reference not set to an instance of an object.

I have checked by simply getting get the tag and the value of the linked textbox and both are populating fine, but i don't want to have to manually write each link in IF Statements. The below code should take the Tag of the checked checkbox E.G txt1 if chk1 is checked and convert it into a TextBox so in the end it would be txtTest.Text = txt1.Text

Dim DisCheckBox As CheckBox() = {chk1, chk2, chk3, chk4, chk5}
For Each Chk In DisCheckBox
If Chk.CheckState = CheckState.Checked Then
txtTest.Text = CType(pnlChk.Controls(Chk.Tag.ToString()), TextBox).Text()
End If
Next

Like i said i have used this before but don't understand why i'm getting this error, any help would be very grateful.

If Not Chk.Tag Is Nothing Then txtTest.Text = CType(pnlChk.Controls(Chk.Tag.ToString), TextBox).Text

Or you could use this and skip using the .Tag of a CheckBox entirely.

'// use "txt" and the # at the end of each CheckBox's Name to locate control.
txtTest.Text = CType(pnlChk.Controls("txt" & Chk.Name.Substring(Chk.Name.IndexOf("k") + 1)), TextBox).Text

If only needing the last TextBox's .Text, why not only set it once?

Dim DisCheckBox As CheckBox() = {chk1, chk2, chk3}
Dim cb As New CheckBox '// sets the checked CheckBox.
For Each Chk In DisCheckBox
If Chk.CheckState = CheckState.Checked Then cb = Chk '// locate the CheckBox if checked.
Next
If Not cb.Name = "" Then '// check if cb has a .Name. gets added if checked.
'// use "txt" and the # at the end of each CheckBox's Name to locate control.
txtTest.Text = CType(pnlChk.Controls("txt" & cb.Name.Substring(cb.Name.IndexOf("k") + 1)), TextBox).Text
End If

i also tried without the Tag and hard coding "txt" instead of the .Tag with no change.

I have now created a new Project to only include what i am trying to do, below is the code and the form has 5 Checkboxes / 5 Textboxes / Button and a Test Textbox. Also entered the Textbox name in the Tag of each Checkbox. I entered a value into the textbox and checked the related textbox and clicked the button

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ChangeCheckBox As CheckBox() = {chk1, chk2, chk3, chk4, chk5}
For Each Chk In ChangeCheckBox
If Chk.CheckState = CheckState.Checked Then
If Not Chk.Tag Is Nothing Then
txtTest.Text = CType(pnlChk.Controls(Chk.Tag.ToString()), TextBox).Text()
End If
End If
Next
End Sub
End Class

I just created a new project with your provided code, added "txt1" to chk1.Tag and a few other CheckBoxes, and even left out some .Tags empty. It did not crash here, ran as it should.
Only time i got it to error with: Object reference not set to an instance of an object. is when I changed the .Tag for chk4 from "txt4" to "test4". Seems that one of your .Tags, if not all, do not have the proper Name(s) of the TextBox(es).

You could add a MsgBox in the For/Next loop to locate which CheckBox cause this issue.

I can't beleive it, i've worked out why it wasn't working. Basically i didn't think it would make a difference but i failed to add that my Chk boxes are in one Panel and my Textboxs are in a different Panel, so obviously it cannot "See" the Textboxes.

So my new question is,

Is there away round this or should i just remove the Panels?

The reason i use the panels is so i can change properties of certain textboxes by looping through the controls of a panel instead of creating an array of controls or typing each control individually.

Thanks for your help codeorder and i most likely will use some of the code you listed in your first post and apologies for not describing the full layout, but i wasn't thinking that it would of made a difference but now it makes sense

There should be no issues if having the CheckBoxes in one Panel and TextBoxes in another. Your code locates the Panel and looks for a certain control within it. txtTest.Text = CType(pnlChk.Controls(Chk.Tag.ToString), TextBox).Text You could add all of your controls to the Form, not Panels, and loop through all of them to locate only your "txt#" TextBoxes.

For Each ctl As Control In Me.Controls
If TypeOf ctl Is TextBox AndAlso IsNumeric(ctl.Name.Substring(ctl.Name.LastIndexOf("t") + 1)) Then
ctl.BackColor = Color.LightSteelBlue
End If
Next

That will locate all TextBoxes that have a # at the end of the .Name and the letter prior to the number is "t".

You could however have the TextBoxes.Names end with "_x" for example, then only check If TypeOf ctl Is TextBox AndAlso ctl.Name.EndsWith("_x") Then .

And i'm even more of an idiot than my first reply, the error was because i didn't fully understand what CType(pnlChk.Controls(Chk.Tag.ToString()), TextBox).Text() was doing, and i was putting the wrong panel in, hence why i thought it couldn't "See" the other panel, anyways all sorted.

Hi. so this is actually a continuation from another question of mine[Here](https://www.daniweb.com/programming/software-development/threads/506795/dynamically-add-values-into-datagridview-cell-from-listbox-vb2010) but i was advised to start a new thread as the original question …

Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...

I have a 2d matrix with dimension (3, n) called A, I want to calculate the normalization and cross product of two arrays (b,z) (see the code please) for each column (for the first column, then the second one and so on).
the function that I created to find the ...