Help: TCP code: NullReferenceException: Object reference not set to an instance of an object.

V

VB Programmer

I am trying to write a telnet emulator. I followed the example code for
EndRead, but when it gets to the last line (in the code below) I get a
NullReferenceException error. Any ideas?

Here's the code:
Private Sub myReadCallBack(ByVal ar As IAsyncResult)
Try
Dim myNetworkStream As NetworkStream = CType(ar.AsyncState,
NetworkStream)
Dim myReadBuffer(1024) As Byte
Dim myCompleteMessage As [String] = ""
Dim numberOfBytesRead As Integer

numberOfBytesRead = myNetworkStream.EndRead(ar) ' ERROR!

The full error: System.NullReferenceException: Object reference not set to
an instance of an object at xxx.Form1.myReadCallBack(IAsyncResult ar).

Any ideas?

Thanks!
 
H

Herfried K. Wagner [MVP]

* "VB Programmer said:
I am trying to write a telnet emulator. I followed the example code for
EndRead, but when it gets to the last line (in the code below) I get a
NullReferenceException error. Any ideas?

Here's the code:
Private Sub myReadCallBack(ByVal ar As IAsyncResult)
Try
Dim myNetworkStream As NetworkStream = CType(ar.AsyncState,
NetworkStream)
Dim myReadBuffer(1024) As Byte

The line above will create a byte array with 1025 elements.
Dim myCompleteMessage As [String] = ""

Remove the square brackets.
Dim numberOfBytesRead As Integer

numberOfBytesRead = myNetworkStream.EndRead(ar) ' ERROR!

The full error: System.NullReferenceException: Object reference not set to
an instance of an object at xxx.Form1.myReadCallBack(IAsyncResult ar).

Have a look at the variable in the watch/local window. Is 'ar' 'Nothing'?
 
V

VB Programmer

I took out the square brackets in the Dim. This had no effect on the error
though.

I've isolated the error to myNetworkStream. Any idea why I get this
NullReferenceException error? 'ar' is NOT nothing. In fact I can print out
the IsCompleted property fine.

Thanks.

Herfried K. Wagner said:
* "VB Programmer said:
I am trying to write a telnet emulator. I followed the example code for
EndRead, but when it gets to the last line (in the code below) I get a
NullReferenceException error. Any ideas?

Here's the code:
Private Sub myReadCallBack(ByVal ar As IAsyncResult)
Try
Dim myNetworkStream As NetworkStream = CType(ar.AsyncState,
NetworkStream)
Dim myReadBuffer(1024) As Byte

The line above will create a byte array with 1025 elements.
Dim myCompleteMessage As [String] = ""

Remove the square brackets.
Dim numberOfBytesRead As Integer

numberOfBytesRead = myNetworkStream.EndRead(ar) ' ERROR!

The full error: System.NullReferenceException: Object reference not set to
an instance of an object at xxx.Form1.myReadCallBack(IAsyncResult ar).

Have a look at the variable in the watch/local window. Is 'ar' 'Nothing'?
 
N

NM

try to pass a reference to your argument :

Private Sub myReadCallBack(ByRef ar As IAsyncResult)
 
G

Guest

The full error: System.NullReferenceException: Object reference not set to
Have a look at the variable in the watch/local window. Is 'ar' 'Nothing'?

Hi there,

I dunno if this helps but the error doesn't say that "ar" is nothing.
It says that a null reference exception occured in your delegate.
Looking at your code, is the netowrk stream object nothing?

It may be casting o.k, but that doesn't mean theres anything to call a
method on. Then when you call EndRead on it, it would thorw the aboe
exception.

hth

Richard
 
G

Guest

I have the newsgroup equivalent of a stutter. i just cant seem to get
it all out in one pa-pa-pa-post.. hopeless.

Note:

Dim myArrayList as ArrayList
myArrayList = CType(Nothing, ArrayList)

is perfectly legal. But when you do

myArrayList.Count

you will get a nullref exception.

I reckon the problem will be in the calling block, your Asynch return
data is null.

hth
Richard
 
V

VB Programmer

Thanks everyone for your suggestions.

NM said:
try to pass a reference to your argument :

Private Sub myReadCallBack(ByRef ar As IAsyncResult)


VB Programmer said:
I am trying to write a telnet emulator. I followed the example code for
EndRead, but when it gets to the last line (in the code below) I get a
NullReferenceException error. Any ideas?

Here's the code:
Private Sub myReadCallBack(ByVal ar As IAsyncResult)
Try
Dim myNetworkStream As NetworkStream = CType(ar.AsyncState,
NetworkStream)
Dim myReadBuffer(1024) As Byte
Dim myCompleteMessage As [String] = ""
Dim numberOfBytesRead As Integer

numberOfBytesRead = myNetworkStream.EndRead(ar) ' ERROR!

The full error: System.NullReferenceException: Object reference not set to
an instance of an object at xxx.Form1.myReadCallBack(IAsyncResult ar).

Any ideas?

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top