0

i'm new working with visual basic, and i've a problem that i can't solve...

 Private Sub Submeter_Click(sender As Object, e As EventArgs) Handles Submeter.Click
    Dim user As New Utilizador
    Dim utilizadores = db.GetTable(Of Utilizador)()


    If (ShortIDTextBox.Text = "") Then
        MessageBox.Show("É necessário inserir um Username.", "Utilizador", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    ElseIf (TextBox1.Text = "") Then
        MessageBox.Show("É necessário inserir um Nome.", "Utilizador", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    ElseIf (MoradaTextBox.Text = "") Then
        MessageBox.Show("É necessário inserir uma morada.", "Utilizador", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    ElseIf (PrefixoComboBox.SelectedItem = "") Then
        MessageBox.Show("É necessário inserir um Prefixo.", "Utilizador", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    ElseIf ((EmailTextBox.Text = "") And (TeleTextBox.Text = "")) Then
        MessageBox.Show("É necessário inserir um email ou telefone.", "Utilizador", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    Else
        Dim query = (From Utilizador In utilizadores Where Utilizador.ShortID = ShortIDTextBox.Text Select Utilizador)

        If (query.Count <> 0) Then
            MessageBox.Show("Já existe um username igual.", "Utilizador", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            user.Nome = TextBox1.Text

            user.morada = MoradaTextBox.Text

            user.Prefixo = PrefixoComboBox.SelectedItem

            user.Email.email = EmailTextBox.Text

            user.Telefone.Telefone = TeleTextBox.Text

            user.ShortID = ShortIDTextBox.Text

            If PrefixoComboBox.SelectedItem.ToString() = "PD" Then
                user.prioridadeCorrente = 1
            End If
            If PrefixoComboBox.SelectedItem.ToString() = "PR" Then
                user.prioridadeCorrente = 2
            End If
            If PrefixoComboBox.SelectedItem.ToString() = "RS" Then
                user.prioridadeCorrente = 3
            End If
            If PrefixoComboBox.SelectedItem.ToString() = "BS" Then
                user.prioridadeCorrente = 3
            End If
            If PrefixoComboBox.SelectedItem.ToString() = "MS" Then
                user.prioridadeCorrente = 3
            End If
            If PrefixoComboBox.SelectedItem.ToString() = "DS" Then
                user.prioridadeCorrente = 3
            End If
            If PrefixoComboBox.SelectedItem.ToString() = "SF" Then
                user.prioridadeCorrente = 3
            End If
            If PrefixoComboBox.SelectedItem.ToString() = "XT" Then
                user.prioridadeCorrente = 3
            End If


            utilizadores.InsertOnSubmit(user)
            db.SubmitChanges()
            Form2.utilizador = user
        End If

        Form2.Show()
        Me.Hide()
    End If

End Sub

This is my code, and when i run the project , it show me no errors, but when I'm filling out the form it breaks and show me the error "An unhandled exception of type 'System.NullReferenceException' occurred" in the line "user.Email.email = EmailTextBox.Text"...

It is necessary to do a try..catch or something?

Thanks!

2
  • 1
    Either the Email property of user is null (Nothing) or the email property of Email is null (Nothing). Put a breakpoint on that line and when the debugger stops on that line, hover your mouse over each of those properties to find out which is null. Commented Jan 2, 2014 at 16:00
  • Probably have to do something like, user.Email = New Email() then set the email property of that object.
    – mafafu
    Commented Jan 2, 2014 at 16:02

1 Answer 1

4

A try/catch won't solve the error, it'll just catch the error and allow you to handle it in some way more graceful than an application crash (which is at least a step in a good direction).

Given the rest of the code, it's highly likely that on this line:

user.Email.email

The .Email property on user is probably what's null (or Nothing in VB). Can you confirm this with some debugging?

If that's the case, then the question becomes what is a Utilizador and how is it initialized? Is it a custom object of yours? What is the type of the .Email property on that object? Given the usage here, it looks like a reference type of some sort.

Reference types are null (Nothing) by default, unless initialized to some instance of the reference type. So what's probably happening here is you have a default constructor which works fine for all of the value types, but never initializes that property. Thus, you can't access that property until it's initialized to something.

Solving this could be as simple as initializing that property in the constructor for Utilizador. Something like this:

Public Sub New()
    Me.Email = New Email()
End Sub

Now, I'm guessing as to the type name being Email. It might not be, you'd have to determine that. (You don't show it in the code currently posted in the question.) But the basic premise here is that reference types need to be initialized before they can be used. And usually the best place to do that is the object's constructor.

Note: user.Telefone will probably have the same problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.