Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
4.6k views
in .NET FTP by (240 points)
I am getting a NullReferenceException when completing a download operation:

EXCEPTION TYPE: System.NullReferenceException
EXCEPTION MESSAGE: Object reference not set to an instance of an object.
STACK TRACE:
at EnterpriseDT.Net.Ftp.FTPTask.ntwLAVRvH()
at EnterpriseDT.Net.Ftp.FTPTask.get_ReturnValue()
at EnterpriseDT.Net.Ftp.ExFTPConnection.EndDownloadByteArray(IAsyncResult asyncResult)

The download is started by calling BeginDownloadByteArray() with our own AsyncCallback. Inside the callback when we call EndDownloadByteArray we receive the above error.

7 Answers

0 votes
by (51.6k points)
Which version are you using?

- Hans (EnterpriseDT)
0 votes
by (240 points)
edtFTPnetPRO v 7.3.1.20
0 votes
by (51.6k points)
I'll try to replicate it. It might help if you paste in the relevant code.

- Hans (EnterpriseDT)
0 votes
by (51.6k points)
I just ran this code
SecureFTPConnection ftp = new SecureFTPConnection();
ftp.ServerAddress = serverAddress;
ftp.UserName = userName;
ftp.Password = password;
ftp.Connect();
IAsyncResult res = ftp.BeginDownloadByteArray(fileName, null, null);
res.AsyncWaitHandle.WaitOne();
byte[] bytes = ftp.EndDownloadByteArray(res);
string s = Encoding.UTF8.GetString(bytes);
System.Diagnostics.Trace.Write(s);
ftp.Close();

and it worked fine.

Could you please run it and see if it works for you? Of course, you'll need to set serverAddress, userName, password and fileName.

- Hans (EnterpriseDT)
0 votes
by (240 points)
void Download(string path, out byte[] contents)
{

// this is a custom object that holds the FTP operation state
FTPState state = new FTPState();

SecureFTPConnection sftpConnection = new SecureFTPConnection();
sftpConnection.ServerAddress = _host;
sftpConnection.UserName = _login;
sftpConnection.Password = _password;
sftpConnection.Protocol = FileTransferProtocol.SFTP;
sftpConnection.ServerValidation = SecureFTPServerValidationType.None;

sftpConnection.Connect();

state.SftpConnection = sftpConnection;
ManualResetEvent waitEvent = state.OperationComplete;

sftpConnection.BeginDownloadByteArray(path, new AsyncCallback(EndBeginDownloadByteArrayCallback), state);

waitEvent.WaitOne();

contents = state.Data;
}

static void EndBeginDownloadByteArrayCallback(IAsyncResult ar)
{
FtpState state = (FtpState)ar.AsyncState;
SecureFTPConnection sftpConnection = state.SftpConnection;

// this call throws the fault
state.Data = sftpConnection.EndDownloadByteArray(ar);
}


This works intermittently. I have a feeling it may have something to do with the thread state, or perhaps with open connections?

Thanks.
0 votes
by (240 points)
One thing I missed is that we set the wait handle after the download is complete. So the callback actually looks like this:
static void EndBeginDownloadByteArrayCallback(IAsyncResult ar)
{
FtpState state = (FtpState)ar.AsyncState;
SecureFTPConnection sftpConnection = state.SftpConnection;

// this call throws the fault
state.Data = sftpConnection.EndDownloadByteArray(ar);

sftpConnection.Close();
state.OperationComplete.Set();

} 


I noticed that there is an AsyncResult that comes out of the BeginDownloadByteArray() call. But, I'm assuming the AsyncCallback event is not triggered until the download is complete? In that case, we wouldn't have to wait on the AsyncResult from BeginDownloadByteArray.
0 votes
by (51.6k points)
This doesn't directly address the problem, but I'm wondering why you're using the asynchronous method when you're really using it synchronously. Since the thread is waiting for the operation to complete immediately after calling BeginDownloadByteArray, you may as well just use the DownloadByteArray method. Also, if you do that then you shouldn't have this problem.

- Hans (EnterpriseDT)

Categories

...