极度郁闷中,,,,,,,,高人进来~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

taoyi 2005-08-19 09:20:04
代码如下:

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处的差别只是加了成员变量的初始化,为啥就这么一加就运行效果就那么大的差别,哪位高人指点一二,,,,,,,偶快疯掉了~~~~~~~~~~~~
...全文
209 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
taoyi 2005-08-20
  • 打赏
  • 举报
回复
哪位老大看看哈,,,我郁闷得很哈~~~,上面几位大哥的建议小弟谢啦,只是都没看到问题的实质哈~
coolsunwind 2005-08-19
  • 打赏
  • 举报
回复
学习中!
JasonHeung 2005-08-19
  • 打赏
  • 举报
回复
改:
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); <=============此处出现"未将对象引用设置为对象的实例"异常
}
TechEye 2005-08-19
  • 打赏
  • 举报
回复
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.
cliff1002 2005-08-19
  • 打赏
  • 举报
回复
最好不要定义同名的变量。
模块级别变量前面可以加个m前缀。
//private string murl = "";
flyboy20 2005-08-19
  • 打赏
  • 举报
回复
帮你顶哈
taoyi 2005-08-19
  • 打赏
  • 举报
回复
郁闷~

public void DownloadEx(string url, string localFile)
{
this.url = url; <==== 这不是赋值了吗?

而且如果我在定义的时候这样声明
private string url = "";

URL不可能是NULL吧,可这样却更是错~~~,每次都出现未将对象引用设置为对象实例,WHY???



110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧