So i have this code in the form called MyMenuForm.

Public Class MyMenuForm

    Public Sub LoadForm(sender As Object, e As EventArgs)
        DataGrid.DataSource = DataGridTable
        DataGridTable.Columns.Add("Name", GetType(String))
        DataGridTable.Columns.Add("Verison", GetType(String))
        DataGridTable.Columns.Add("Compile", GetType(Button))
        DataGridTable.Columns.Add("Location", GetType(String))
        DataGridTable.Columns.Add("CompileLoc", GetType(String))
    End Sub

    Public DataGridTable As DataTable

End Class

I want to be able to edit DataGridTable from a different form called AddForm.

Public Class AddForm

    Public Sub Add_Click(sender As Object, e As EventArgs) Handles AddButton.Click
        MyMenuForm.DataGridTable.Rows.Add(NameBox(), VersionBox(), "Compile", LocationBox(), CompileBox())
    End Sub

End Class

When I click on the AddButton button, I receive the error

Additional information: Object reference not set to an instance of an object.

Does anyone know why this happens or how I can fix it? I have googled to the extent of my ability and have found no solution. Please Help.

Try out this test project I created for this example

Here's a little bit of explanation:

It's very important to pay attention to scope. Object reference not set to an instance of an object is a very common error, one that usually indicates a need for some kind of architectural adjustment.

Here's the setup for the MyMenuForm class. The DataTable is declared as a property of that class, so anybody who can access the class, can access that particular property.

<!-- language: lang-vb -->
Public Class MyMenuForm

    Public DataGridTable As New DataTable

    Private Sub LoadForm(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        With DataGridTable.Columns
            .Add("Name", GetType(String))
            .Add("Verison", GetType(String))
            .Add("Compile", GetType(Button))
            .Add("Location", GetType(String))
            .Add("CompileLoc", GetType(String))
        End With
        DataGridView1.DataSource = DataGridTable
    End Sub

End Class

You'll also need to make sure that the MyMenuForm has been created before you try to add rows with the AddForm class. In my case, I just added it as the startup form and opened up an add form on click

Startup From

In the AddForm, make sure you reference the DataGridTable property on the MyMenuForm class, like so:

<!-- language: lang-vb -->
Private Sub AddButton_Click(sender As System.Object, e As System.EventArgs) Handles AddButton.Click
    Dim row As DataRow = MyMenuForm.DataGridTable.NewRow()

    With row
        .Item("Name") = "TestName"
        .Item("Verison") = "TestVerison"
        .Item("Compile") = New Button
        .Item("Location") = "TestLocation"
        .Item("CompileLoc") = "TestCompileLoc"
    End With

    MyMenuForm.DataGridTable.Rows.Add(row)

End Sub