Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net WebClient.OpenRead异常:";值不能为空。”;_.net_Vb.net_Exception_Webclient - Fatal编程技术网

.net WebClient.OpenRead异常:";值不能为空。”;

.net WebClient.OpenRead异常:";值不能为空。”;,.net,vb.net,exception,webclient,.net,Vb.net,Exception,Webclient,我正在开发一个刮板,它可以刮取一个网页上的链接,然后创建线程来执行 刮削子页面 这就是线程所做的: Dim client As New WebClient() Dim stream As Stream = client.OpenRead(_Address) Dim streamReader As New StreamReader(stream, True) _Content = streamReader.ReadToEnd() streamReader.Close() streamReader.

我正在开发一个刮板,它可以刮取一个网页上的链接,然后创建线程来执行 刮削子页面

这就是线程所做的:

Dim client As New WebClient()
Dim stream As Stream = client.OpenRead(_Address)
Dim streamReader As New StreamReader(stream, True)
_Content = streamReader.ReadToEnd()
streamReader.Close()
streamReader.Dispose()
stream.Close()
stream.Dispose()
client.Dispose()
我注意到,有时(通常是当有更多同时运行的线程时),一个线程会抛出一个异常。它是随机发生的,异常被抛出到
客户端.OpenRead
,它说
“值不能为null。参数名:address”
。我还有一个try..catch,所以我在catch块中放置了一个断点,看起来
\u地址当时是有效的,但是代码抛出了一个异常

\u Address
是受保护的类字段,其他线程无法访问

确切的信息是:

“值不能为空。参数名称:地址”

异常为
System.ArgumentNullException

堆栈跟踪是:

在MyAppFolder\Scraper.vb中MyAppName.Scraper.Scrape()处的System.Net.WebClient.OpenRead(字符串地址):第96行

你对解决这个问题有什么建议吗?
提前感谢。

WebClient.OpenRead(字符串地址)
的内部实现只是:

public Stream OpenRead(string address)
{
    if (address == null)
    {
        throw new ArgumentNullException("address");
    }
    return this.OpenRead(this.GetUri(address));
}
因此,
\u Address
传入时必须为null

也许可以试试这样:

private string _address;
private string _Address
{
    get
    {
        if(_address == null)
            throw new ArgumentNullException("_Address was never set and is still null!");
        return _address;
    }
    set
    {
        if(value == null)
            throw new ArgumentNullException("_Address can not be null!");
        _address = value;
    }
}

因此,基本上,如果有东西试图将_Address设置为null,那么当它发生时,您会得到一个正确的错误,并且可以在调用堆栈中看到它被设置为null的位置。

您是否在静态(模块/共享)类/方法中使用此代码?发布错误消息和stacktrace将是一个好主意。因为它是关于_Address的,它是从哪里来的?@Oded No,公共类方法中的代码。@Henk Holterman确切的消息是“值不能为null。参数名:Address”。异常为System.ArgumentNullException。堆栈跟踪是:“在MyAppFolder\Scraper.vb:line 96”@Witchunter中MyAppName.Scraper.Scrape()的System.Net.WebClient.OpenRead(字符串地址)处,请编辑您的问题,并在问题中添加堆栈跟踪和其他信息。在评论中,它可能会被掩盖,而且显然更难阅读。我投票支持你,因为我会这么做:检查空值,或者立即抛出异常,或者在逻辑要求的情况下依赖默认值。只是为了将来的屈膝礼,请在发布之前转换到VB.NET(无论是什么转换器),因为这被标记为“VB.NET”。谢谢