Reputation: 661
First, I know this is a very common error! I have dealt with and fixed this error message before, and I understand what it means, however in this case I cannot figure out what I am doing wrong. I did make an effort to search for similar answers first. I will very specifically describe my problem below, and make an earnest effort to understand the concepts involved herein.
Here is my MainForm.vb, the Form which pops up when running the Windows Forms app:
Public Class MainForm
Inherits System.Windows.Forms.Form
Private Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim f2 As New Introduction
MainPanel.Controls.Add(f2)
f2.Show()
End Sub
' This is used for keeping track of forward/backward data:
Public Property FormDataCollection As DataSet
Get
Return Me.FormData.Copy '<-- Issue
End Get
Set(value As DataSet)
Me.FormData = value
End Set
End Property
Public FormData As DataSet
End Class
As you can see, I have created a DataSet collection as a property of the main Form of the application. I will access this property later on to Add/Remove tables to the DataSet to implement Forward/Back buttons as well as keeping track of AutoFill information. However, I run into a problem when I try to access the property later on:
Public Class Introduction
Inherits System.Windows.Forms.UserControl
Private Sub Introduction_Load(sender As System.Object, e As System.EventArgs) Handles Me.Load
End Sub
Private Sub AddOrRemoveButton_Click(sender As System.Object, e As System.EventArgs) Handles AddOrRemoveButton.Click
Dim AddRemoveCompanyControl = New AddRemoveCompany
MainForm.MainPanel.Controls.Clear()
MainForm.MainPanel.Controls.Add(AddRemoveCompanyControl)
AddRemoveCompanyControl.Show()
Dim AddRemoveCompany As DataTable = New DataTable("AddRemoveCompany")
MainForm.FormDataCollection.Tables.Add(AddRemoveCompany) 'ERROR T-Minus 10, 9, 8...
End Sub
End Class
When I try to do a Add to the DataSet, I get the error. I am not sure how this can be, as I am clearly creating a copied instance of the DataSet in my Get part of the property.
Upvotes: 1
Views: 615
Reputation: 81675
Your collection isn't initialized:
Public FormData As New DataSet
That field should probably be private since you are accessing it through a public property.
Upvotes: 1