• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 143
  • Last Modified:

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



0
VBdotnet2005
Asked:
VBdotnet2005
  • 3
  • 2
2 Solutions
 
Fernando SotoCommented:
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
 
Fernando SotoCommented:
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
0
 
Fernando SotoCommented:
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.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now