可能出现空引用异常

发布于 2024-08-24 00:54:35 字数 741 浏览 3 评论 0原文

Resharper 显示“可能的 System.NullReferenceException”警告。然而我不知道如何才能得到一个。

public class PlaceController : PlanningControllerBase
{
    [Authorize]
    public ActionResult StartStop(int id)
    {
        if (Request != null && Request.Cookies != null && Request.Cookies["place"] != null)
        {
            if (Request.Cookies["place"].Value != null)//Possible NullReferenceException?
            {
                string placeInformation = Request.Cookies["place"].Value;//Possible NullReferenceException?
                //...
            }
        }
    }
}

如果我检查所有字段,这如何给出 NullReference?使用以下内容不会显示警告:

Request.Cookies[0];//Index instead of name

编辑:更新的代码。

Resharper is showing a "Possible System.NullReferenceException" warning. I however can't see how I can get one.

public class PlaceController : PlanningControllerBase
{
    [Authorize]
    public ActionResult StartStop(int id)
    {
        if (Request != null && Request.Cookies != null && Request.Cookies["place"] != null)
        {
            if (Request.Cookies["place"].Value != null)//Possible NullReferenceException?
            {
                string placeInformation = Request.Cookies["place"].Value;//Possible NullReferenceException?
                //...
            }
        }
    }
}

How can this give a NullReference if I check all fields? Using the following doesn't show the warning:

Request.Cookies[0];//Index instead of name

Edit: updated code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

残疾 2024-08-31 00:54:35

我假设检查器不会检查每次传递给 CookieCollection 索引器的字符串值是否相同。我想如果您将代码重组为:

if (Request != null && Request.Cookies != null) 
{
    var place = Request.Cookies["place"];
    if (place != null && place.Value == null) 
    { 
        string placeInformation = place.Value;
    } 
}

它可能会起作用。

I assume the checker isn't checking the value of the string passed to the CookieCollection indexer is the same each time. I imagine if you restructure the code to:

if (Request != null && Request.Cookies != null) 
{
    var place = Request.Cookies["place"];
    if (place != null && place.Value == null) 
    { 
        string placeInformation = place.Value;
    } 
}

It might work.

一张白纸 2024-08-31 00:54:35

您不需要听取每一个警告。 Request 对象和 Cookies 对象永远不会为 null,因此这就是您所需要的。

var placeCookie = Request.Cookies["place"]; 
if (placeCookie != null)
{
    string placeInformation = placeCookie.Value;
}

You don't need to listen to every single warning. The Request object and the Cookies object will never be null so this is all you need.

var placeCookie = Request.Cookies["place"]; 
if (placeCookie != null)
{
    string placeInformation = placeCookie.Value;
}
九命猫 2024-08-31 00:54:35

呃,你不想 Request.Cookies["place"].Value != null
,现在您只需将 placeInformation 设置为 null。

err don't you want Request.Cookies["place"].Value != null
, right now you will only be setting placeInformation to null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文