Link to home
Start Free TrialLog in
Avatar of VBdotnet2005
VBdotnet2005Flag for United States of America

asked on

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



Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

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
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial