We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Interface  - 2

VBdotnet2005
VBdotnet2005 asked
on
Medium Priority
156 Views
Last Modified: 2010-04-23
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



Comment
Watch Question

Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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 SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.