locked
Null Reference Exception RRS feed

  • Question

  • This is my code:

     

    Code Snippet

    Private Sub ConnectCertain()

    win.RemoteHost = txtIP.Text

    win.LocalPort = 35

    win.Connect()

    win.Listen()

    win.Close()

    End Sub

     

    But everytime I debug it it says something about a NullReferenceException and won't continue. I clicked on all the links but nothing seemed to help I  then changed the code to win.NewLocalPort as shown below:

     

    Code Snippet

    Private Sub ConnectCertain()

    win.RemoteHost = txtIP.Text

    win.NewLocalPort = 35

    win.Connect()

    win.Listen()

    win.Close()

    End Sub

     

    This still didn't solve it has anyone got a solution. Please don't give me links alone as none of them make sense but explanations are welcome.

     

    Tuesday, May 1, 2007 7:45 PM

Answers

  • Where do you define the variable "win" and what data type is it.

     

    Almost certainly you havent instantiated an instance of the data Type for the variable win.

     

    Normally this is evident by a line something like

    Code Snippet

     

    Dim Win as NEW xxxxx

     

     

    where xxxxx is the data type

     

    The new keyword instantiates an instance of the type, creating an object.

     

    If you have something like

     

    Code Snippet

    Dim Win as xxxxx

     

     

    Then you have created a variable which can hold a reference to an object of type xxxxx but its not set to anything, and is therefore set to nothing (null) so when you try and use it you will generate a null reference exception.

     

    So your going to need to look back in your code to find where win is actually instantiated.

     

     

     

    Tuesday, May 1, 2007 7:58 PM

All replies

  • Where do you define the variable "win" and what data type is it.

     

    Almost certainly you havent instantiated an instance of the data Type for the variable win.

     

    Normally this is evident by a line something like

    Code Snippet

     

    Dim Win as NEW xxxxx

     

     

    where xxxxx is the data type

     

    The new keyword instantiates an instance of the type, creating an object.

     

    If you have something like

     

    Code Snippet

    Dim Win as xxxxx

     

     

    Then you have created a variable which can hold a reference to an object of type xxxxx but its not set to anything, and is therefore set to nothing (null) so when you try and use it you will generate a null reference exception.

     

    So your going to need to look back in your code to find where win is actually instantiated.

     

     

     

    Tuesday, May 1, 2007 7:58 PM
  • Win is a winsock control and what you said worked

    thanx a shmillion!!!!!!!!!!!!!!!!

    Tuesday, May 1, 2007 9:01 PM