Interface - 2

Why wouldn't this code work? I kind of modified it a bit from a sample at informit.com, one of the expert's suggestion

An unhandled exception of type 'System.NullReferenceException' occurred in Interface.exe

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

...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim xSingleEn As New InterfacePlane.SingleEngineLand

Me.Label1.Text = xSingleEn.TailNumber(Me.TextBox1.Text).ToString







End Sub



.......









Namespace InterfacePlane



Public Interface plane

Property TailNumber(ByVal x As String) As String

Property TailNumer2(ByVal y As String) As String





End Interface

Public Class SingleEngineLand

Implements plane







Private t1 As String

Private t2 As String





Public Property TailNumber(ByVal x As String) As String Implements plane.TailNumber

Get

Return t1



End Get

Set(ByVal Value As String)

t1 = Value





End Set

End Property

Public Property TailNumer2(ByVal y As String) As String Implements plane.TailNumer2

Get

Return t2



End Get

Set(ByVal Value As String)

t2 = Value



End Set

End Property

End Class





End Namespace



VBdotnet2005Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi VBdotnet2005;

The problem you are having is this. In the SingleEngineLand class you have no constructor that initializes the two variables t1 and t2. When you create the instance of SingleEngineLand the two variables are set to Nothing. This is why you are getting the error "System.NullReferenceException". To correct the problem you can create a constructor and initialize the the variables like this.

Public Sub New()
    t1 = ""
    t2 = ""
End Sub

or initialize the values before using them. Somthing like this:

xSingleEn.TailNumber("") = ""
xSingleEn.TailNumber2("") = ""

Fernando
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
I think it should look more like this...

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim xSingleEn As New InterfacePlane.SingleEngineLand
        xSingleEn.TailNumber = Me.TextBox1.Text
        Me.Label1.Text = xSingleEn.TailNumber.ToString
    End Sub


Namespace InterfacePlane

    Public Interface plane
        Property TailNumber() As String
        Property TailNumer2() As String
    End Interface

    Public Class SingleEngineLand
        Implements plane

        Private t1 As String
        Private t2 As String

        Public Property TailNumber() As String Implements plane.TailNumber
            Get
                Return t1
            End Get

            Set(ByVal Value As String)
                t1 = Value
            End Set
        End Property

        Public Property TailNumer2() As String Implements plane.TailNumer2
            Get
                Return t2
            End Get

            Set(ByVal Value As String)
                t2 = Value
            End Set
        End Property

    End Class

End Namespace

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
Hi Idle_Mind;

The two properties of that class takes 1 parameter so the following will give the error "Argument not specified for parameter 'x'.

        xSingleEn.TailNumber = Me.TextBox1.Text
        Me.Label1.Text = xSingleEn.TailNumber.ToString

You will need to do something like this:

        xSingleEn.TailNumber("SomeValue") = Me.TextBox1.Text
        Me.Label1.Text = xSingleEn.TailNumber("SomeValue").ToString

for it to compile and run.

Fernando
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Hi Fernando...

I completely changed the definitions...take a closer look at my post.  =)

I don't understand why "x" and "y" are being passed into a Property.
Fernando SotoRetiredCommented:
Sorry my error. I did not look at the SingleEngineLand class and interface.

I did not know either why he was passing in the parameters but figured he will be using it somehow.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.