Resolved Creating Textbox Array: System.NullReferenceException

jcardana

Old to VB6, New to VB.NET
Joined
Oct 26, 2015
Messages
43
Location
Rio Rancho, NM
Programming Experience
Beginner
I'm attempting to create an array of textboxes. I need 55 of them (5 banks of 11).
After searching the internet , I came across two examples @ How to create Control Arrays in VB .NET
I was able to make it work successfully once but I couldn't figure out how to make the Events work, so I reverted.
Then I decided to try again and now I'm stuck.

jmcilhinney posted in another thread...
It means that something on the lefthand side of a dot is Nothing.
In this case, it means txtPreset(i) is Nothing but I don't now how to make it not Nothing.
I've looked up Instancing, Declaring, Creating and didn't understand anything.

In the Error "details" I see...
Document
NameValueType
headNothingSystem.Collections.ListDictionaryInternal.DictionaryNode
I saw something about a Dictionary on another website. Do I need to add a Dictionary entry?

Thanks for any and all help,
Joe

VB.NET:
Public Class frmPresetRename

    Public txtPreset(54) As TextBox

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        txtPreset(54) = New TextBox
    End Sub

    Private Sub frmPresetRename_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For i As Integer = 0 To 54
            Dim txtPreset(i) As TextBox
            With txtPreset(i)
                .TextAlign = HorizontalAlignment.Center ' <-- System.NullReferenceException: 'Object reference not set to an instance of an object.'
                .Font = New Drawing.Font("Consolas", 10, False, False)
                .Top = 64 + (32 * (i Mod 11))
                .Left = 48 + Int(i / 11) * 128
                .Text = Chr((i \ 11) + Asc("A")) & i Mod 11 + 1
                .Visible = True
            End With
            Controls.Add(txtPreset(i))
        Next
    End Sub
End Class
 
Solution
Since you made me go back, THINK (thank you) and resolve...
VB.NET:
    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        For i = 0 To 54
            txtPreset(i) = New TextBox
        Next
    End Sub
I was only creating txtpreset(54) not ALL 55 of them
Line 13 is declaring (Dim) a local variable with same name as your field.

Take a look at these two articles:
 
I changed line 13 to...
VB.NET:
            Dim txtPreset() As New TextBox
I get Error BC30053 Arrays cannot be declared with 'New'
 
Do you really want to declare a new local variable named txtPreset? or do you want to assign something to your existing one? Dim means declaring a variable...
 
Do you really want to declare a new local variable named txtPreset? or do you want to assign something to your existing one? Dim means declaring a variable...

I'm trying to assign values to the properties with the With block.
I removed line 13.
The error tells me, there's no txtPreset(i) to provide values to.
My understanding (or lack thereof) tells me line 8 is supposed to be creating the array of 55 textboxes. The For Next loop, is supposed to be positioning them.
I've never had to create an array of controls at runtime since VB6 so this is kicking my butt.
Either way this goes, I thank you for your time and patience.
 
I removed line 13.

Don't do that. Just change it.
First, do not declare a variable, that means removing the Dim.
Second, you're right in using New operator to create a Textbox.
Then you need to use the assignment operator = to assign you new textbox to the right slot in existing txtPreset array.
 
Since you made me go back, THINK (thank you) and resolve...
VB.NET:
    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        For i = 0 To 54
            txtPreset(i) = New TextBox
        Next
    End Sub
I was only creating txtpreset(54) not ALL 55 of them
 
Solution

Latest posts

Back
Top