代码如下: public class Ftp
{
#region 私有成员
//-------------------------------------------------------------------------------
 1.
//private string host = "";
//private string user = "";
//private string password = "";
//private int port = 21;
//private string url = "";
//private string destPath = "";
 //-----------------------------------------------------------------------------
 2.
private string host;
private string user;
private string password;
private int port = 21;
private string url;
private string destPath;
//-------------------------------------------------------------------------------
private static MessageCallBackFunc lpMsg = null;
private static PacketRecvCallBackFunc lpPackRecv = null;
private static FileCompleteCallBackFunc lpFileComp = null;
#endregion #region 回调函数
public delegate void MessageCallBackFunc(string msg);
public delegate void PacketRecvCallBackFunc(string file, int totalBytes, int bytesRecved);
public delegate void FileCompleteCallBackFunc(string file);
#endregion #region API
[DllImport("FTP.dll", EntryPoint="FtpDownload",CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]
private static extern void _Download(string host, string user, string pass, 
int nPort, string url, string locaFile,
MessageCallBackFunc lpMsg, PacketRecvCallBackFunc lpPackRecv,
FileCompleteCallBackFunc lpFileComp);
#endregion #region 属性
public String Host
{
get
{
return this.host;
}
set
{
if (value.Length == 0)
throw new Exception("Host空参数异常");
this.host = value;
}
} public String UserID
{
get
{
return this.user;
}
set
{
if (value.Length == 0)
throw new Exception("UserID空参数异常");
this.user = value;
}
} public String Password
{
get
{
return this.password;
}
set
{
if (value.Length == 0)
throw new Exception("Password空参数异常");
this.password = value;
}
} public int Port
{
get
{
return this.port;
}
set
{
if (value == 0)
this.port = 21;
else
this.port = value;
}
} public string DownFile
{
get
{
return this.url;
}
set
{
if (value.Length == 0)
throw new Exception("DownFile空参数异常");
this.url = value;
}
} public string DestPath
{
get
{
return this.destPath;
}
set
{
if (value.Length == 0)
throw new Exception("DestPath空参数异常");
this.destPath = value;
}
}
#endregion #region 构造及析构
public Ftp(MessageCallBackFunc lpMsg, PacketRecvCallBackFunc lpPackRecv, FileCompleteCallBackFunc lpFileComp)
{
if (lpMsg != null)
lpMsg = lpMsg;
if (lpPackRecv != null)
lpPackRecv = lpPackRecv;
if (lpFileComp != null)
lpFileComp = lpFileComp;
} ~Ftp()
{
}

#endregion #region 方法申明 public void DownloadEx(string url, string localFile)
{
this.url = url;
Ftp._Download(this.host, this.user, this.password, this.port, this.url, localFile,
lpMsg, lpPackRecv, lpFileComp);   <=============此处出现"未将对象引用设置为对象的实例"异常
} public void DownloadEx()
{
string shortname = this.url.Substring(this.url.LastIndexOf("/") + 1, 
this.url.Length - this.url.LastIndexOf("/") -  1);

Ftp._Download(this.host, this.user, this.password, this.port, this.url, this.destPath + "\\" + shortname,
lpMsg, lpPackRecv, lpFileComp);
}
}如果将上面1处注释的代码替换为2外的代码,则在_Download调用处出现未将对象引用设置为对象实例的异常,如果使用2处的代码,则_Download有时会出现同样的异常,但却没有规律,,,,,极度郁闷中~~~~~代码1处和2处的差别只是加了成员变量的初始化,为啥就这么一加就运行效果就那么大的差别,哪位高人指点一二,,,,,,,偶快疯掉了~~~~~~~~~~~~

解决方案 »

  1.   

    public void DownloadEx(string url, string localFile)
    {
    this.url = url;
    Ftp._Download(this.host, this.user, this.password, this.port, this.url, localFile,
    lpMsg, lpPackRecv, lpFileComp); <=============此处出现"未将对象引用设置为对象的实例"异常
    }楼主,以上方法中,你有参数也叫url,你全局又定义了private string url;
    楼主在Ftp._Download(..中用了 this.url);
    this.url是指你那个全局变量,它的确是null的!!!!!
    你的意思是调用参数吧,把this.url改成url.
      

  2.   

    改:
    public void DownloadEx(string _url, string localFile)
    {
    this.url = _url;
    Ftp._Download(this.host, this.user, this.password, this.port, this.url, localFile,
    lpMsg, lpPackRecv, lpFileComp); <=============此处出现"未将对象引用设置为对象的实例"异常
    }
      

  3.   

    郁闷~public void DownloadEx(string url, string localFile)
    {
    this.url = url;  <==== 这不是赋值了吗?而且如果我在定义的时候这样声明
    private string url = "";URL不可能是NULL吧,可这样却更是错~~~,每次都出现未将对象引用设置为对象实例,WHY???
      

  4.   

    最好不要定义同名的变量。
    模块级别变量前面可以加个m前缀。
    //private string murl = "";