1

Why does the code pasted below throw me a Null Reference Exception? Because I simply can't seem to find out why.

Note: The textboxes have the correct names & they aren't empty when I get the exception.

My code: (I've highlighted the line with the error)

txtGuid.BackColor = Color.White
    txtName.BackColor = Color.White
    If txtGuid.Text <> "" And txtName.Text <> "" Then
        Dim name As String = txtName.Text
        Dim guid As String = txtGuid.Text
        **frmWhitelist.names.Add(name)**
        frmWhitelist.guids.Add(guid)
        Me.Close()
    Else
        If txtGuid.Text = "" Then
            txtGuid.BackColor = Color.Red
        End If
        If txtName.Text = "" Then
            txtName.BackColor = Color.Red
        End If
        lblError.Text = "Please check your input in the red-colored fields."
    End If

Call stack:

> DayZAdminApp.exe!DayZAdminApp.inptBoxWhitelist.btnOk_Click(Object sender, System.EventArgs e) Line 15 + 0x3d bytes Basic

While debugging, the txtGuid.text and txtName.text both have text values in my Locals tab.

PS: If this is caused by the fact that I'm trying 2 add items to a public var on another form, how can I work around this?

PPS: The frmWhitelist.namesand frmWhitelist.guids are declared as such:

Public names, guids As List(Of String)

Yorrick
  • 377
  • 2
  • 6
  • 27
  • Could you tell us which one has the NullReferenceException? You can easily check this with the debugger attached – Kenneth Apr 13 '13 at 21:28
  • `frmWhitelist.names.Add(name)` – Yorrick Apr 13 '13 at 21:28
  • I guess then frmWhitelist or the names-collection of it are null is null. Where does it get created? – Kenneth Apr 13 '13 at 21:30
  • It's called from a button, that form works just fine & that's the form which calls the form with this code. (I'm sort of using it as a custom inputbox with 2 inputs) – Yorrick Apr 13 '13 at 21:31
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders May 15 '14 at 02:59

1 Answers1

2

The lists are Nothing since you haven't initialized them.

So you get an exception here:

frmWhitelist.names

This avoids the exception:

Public names As New List(Of String)
Public guids As New List(Of String)

You are just declaring them here:

Public names, guids As List(Of String)

They are still Nothing at this point even if they are declared.

Tim Schmelter
  • 411,418
  • 61
  • 614
  • 859
  • Right, I'm an idiot, I knew I should've done that, but I couldn't remember why, thanks :) – Yorrick Apr 13 '13 at 21:32
  • You still need to assign them to the names collection: frmWhitelist.names = names frmWhitelist.guids = guids – Kenneth Apr 13 '13 at 21:33
  • @Kenneth: I don't understand. Both lists are declared in a different form. OP wants to add strings here `frmWhitelist.names.Add(name)`. – Tim Schmelter Apr 13 '13 at 21:34
  • Sorry, my bad, I thought he had declared them locally. If he has declared them as auto-properties in the other form then he should be fine. – Kenneth Apr 13 '13 at 21:35