标签: nullreferenceexception

处置实现IDisposable的成员

在我的Dispose方法(如下所示)中,每次我想调用someObj.Dispose()时我都会检查someObj!= null.

这是因为我的设计不好吗?他们是否更清楚地确定在没有NullReference异常风险的情况下调用在对象中使用的所有成员(实现IDisposable)的Dispose?

protected void Dispose(bool disposing)
        {
            if (disposing)
            {
               if (_splitTradePopupManager != null)
                {
                    _splitTradePopupManager.Dispose();
                }
             }
        }
Run Code Online (Sandbox Code Playgroud)

谢谢你的关注.

c# idisposable nullreferenceexception

9
推荐指数
2
解决办法
2099
查看次数

Catch NullReferenceException还是先测试Nothing?

我们有一个属性,其工作是查找描述.如果查找失败,则应显示空字符串.

所以我们可以像这样对属性进行编码:

If foo.bar Is Not Nothing Then
  Return foo.bar.Description
Else
  Return String.Empty
End If
Run Code Online (Sandbox Code Playgroud)

但是这涉及到两次执行foo.bar,如果这样做很昂贵,那么它可能更好:

Dim b As bar = foo.bar
If b IsNot Nothing Then
  Return b.Description
Else
  Return String.Empty
End If
Run Code Online (Sandbox Code Playgroud)

但实际上我们想要做的就是将任何类型的错误视为空的描述.所以在某些方面这更简单:

Try
  Return foo.bar.Description
Catch e As NullReferenceException
  Return String.Empty
End Try
Run Code Online (Sandbox Code Playgroud)

但是,有没有问题(性能,纯度,其他?)只是捕捉和忽略错误?

你有时会认为抛出异常昂贵的,但是我不确定作者是否意味着使用Throw关键字(我没有做的)构建异常是昂贵的,或者他是否意味着允许异常发生是很昂贵的(因为我会这样做).

vb.net nullreferenceexception

9
推荐指数
1
解决办法
2万
查看次数

为什么这会引发空引用异常?

当InnerException为null时,这将抛出一个空引用异常.

String s = " inner exception: " + e.InnerException == null ? "None" : e.InnerException.Message;
Run Code Online (Sandbox Code Playgroud)

但这不会:

String s = " inner exception: " + (e.InnerException == null ? "None" : e.InnerException.Message);
Run Code Online (Sandbox Code Playgroud)

以上两种都很好.我无法弄清楚前者试图做什么会导致它进行评估e.InnerException.Message.为什么它们不相同?

c# conditional-operator nullreferenceexception

9
推荐指数
1
解决办法
1074
查看次数

当已经检查null时,ReSharper可能为Null Exception

这是使用Visual Studio 2012的ReSharper 7.下面的示例

// This code works fine and as expected and ReShrper is happy with it
if (!string.IsNullOrWhiteSpace(extension) && extension.Length == 3)
{
    // do something
}

// ReSharper highlights "extension" in extension.Length with "Possible 'System.NullReferenceException'"
if (!extension.IsNullOrWhiteSpace() && extension.Length == 3)
{
    // do something
}
Run Code Online (Sandbox Code Playgroud)

并且,我创建了以下扩展方法:

public static class StringExtensions
{
    public static bool IsNullOrWhiteSpace(this string s)
    {
        return string.IsNullOrWhiteSpace(s);
    }
}
Run Code Online (Sandbox Code Playgroud)

我查看了反映的代码,String.IsNullOrWhiteSpace它没有任何相关的代码或属性,这些代码或属性会突出显示R#验证检查.这是硬编码在R#?

我查看了代码合同,但我不确定它会对我的情况有所帮助.

您是否有一种解决方法可以向ReSharper证明我的扩展方法已经验证了检查条件?

c# resharper extension-methods code-contracts nullreferenceexception

9
推荐指数
1
解决办法
1862
查看次数

在WCF服务方法中使用JSON

在一个较大的项目中,我无法获得WCF服务方法来使用JSON参数.所以我制作了一个较小的测试用例,并且行为得到了回应.如果我调试服务,我可以看到服务调用时参数值为null.Fiddler确认正在发送JSON,JsonLint确认它是有效的.

下面的代码带有调试注释.

[ServiceContract]
public interface IWCFService
{

    [OperationContract]
    [WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest,
            ResponseFormat = WebMessageFormat.Json,
            UriTemplate = "getstring")]

    string GetString();

    [OperationContract]
    [WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest,
        ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "getplayer")]
    //[WebGet(BodyStyle = WebMessageBodyStyle.WrappedRequest,
    //    ResponseFormat = WebMessageFormat.Json,
    //    UriTemplate = "getplayers")]
    Player GetPlayer();

    [OperationContract]
    [WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest,
        ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "getplayers")]
    List<Player> GetPlayers();

    [OperationContract]
    [WebInvoke(
        Method = "POST",
        BodyStyle = WebMessageBodyStyle.Wrapped,
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json,
        UriTemplate = "totalscore")]
    string TotalScore(Player player);

}
Run Code Online (Sandbox Code Playgroud)

......及其实施

public class WCFService : …
Run Code Online (Sandbox Code Playgroud)

javascript wcf jquery json nullreferenceexception

8
推荐指数
1
解决办法
2万
查看次数

如何修复Orchard CMS中的"对象引用未设置为对象的实例"错误

我正在建立一个果园CMS网站.最初,我下载了压缩版本,但是VS在项目Markdown.csproj第1行中抱怨错误:"对象引用未设置为对象的实例".这对我没有意义,所以我尝试从Web安装它平台安装程序 这有点好用了一段时间.然后我在一些主题更改后重新启动visual studio并再次得到原始错误.我试图恢复主题更改并尝试在管理模式下重新启动VS. 仍然没有骰子.

我可以通过谷歌找到Markdown.csproj的主题.如果有人有建议,我会全力以赴.谢谢.

nullreferenceexception orchardcms

8
推荐指数
1
解决办法
1万
查看次数

为什么ReSharper在动态类型上建议可能的NullReferenceException?

如果我写下面的代码,ReSharper会警告我有可能NullReferenceException.但是我null在上面的陈述中明确地检查了.有什么关于dynamic我不知道的事情(假设它可能是由某个IEnumerable或类似的东西支持)?或者这是ReSharper的故障?或者是其他东西?

dynamic user = connection.Query("SELECT ...").FirstOrDefault(); // Dapper Extension
if (user == null)
    return null;

return new User(user.username);
//              ^^^^
// (local variable) dynamic user
//
// Possible 'System.NullReferenceException'
Run Code Online (Sandbox Code Playgroud)

c# resharper dynamic nullreferenceexception

8
推荐指数
1
解决办法
1512
查看次数

为什么ArgumentNullException?为什么不System.NullReferenceException?

请参阅以下代码行:

DataTable [] _tables = null;

// Throws System.NullReferenceException
_tables.GetType();

// Throws System.ArgumentNullException
_tables.Count();
Run Code Online (Sandbox Code Playgroud)

在这行代码中我_tables引用并试图访问它的系统定义函数,GetType()并且Count()都抛出异常,但为什么.Count()抛出System.ArgumentNullException,因为我们有相同的参考值,是null什么?

c# argumentexception nullreferenceexception

8
推荐指数
2
解决办法
1018
查看次数

Roles.GetRolesForUser抛出异常未将对象引用设置为对象的实例

我有一个使用该Roles.GetRolesForUser方法的ASP.NET应用程序.调用工作在应用程序中罚款,但是当我在引用的库中使用相同的调用时,它会抛出异常.异常消息是:

你调用的对象是空的

奇怪的是,当我检查角色时,它会被实例化.

我的代码看起来像这样:

var roles = Roles.GetRolesForUser(userName);
Run Code Online (Sandbox Code Playgroud)

有什么建议?

c# asp.net asp.net-mvc roles nullreferenceexception

8
推荐指数
2
解决办法
2361
查看次数

如果我要立即使用它,我应该检查参数是否为null?

我习惯于检查方法的参数是否为null(然后继续抛出异常),我几乎不再考虑它了.如果参数是引用类型,那么它就在那里:

if(arg == null)
    throw new ArgumentNullException(nameof(arg));
Run Code Online (Sandbox Code Playgroud)

但是,如果我要立即使用arg怎么办?我应该检查一下吗?我的意思是,如果我不这样做,无论如何,envinroment将为我抛出(NullReferenceException).

例如:

public int DoStuff(object o)
{
    return o.GetHashCode();
}
Run Code Online (Sandbox Code Playgroud)

我可以轻松地添加null检查:

public int DoStuff(object o)
{
    if(o == null)
        throw new ArgumentNullException(nameof(o));
    return o.GetHashCode();
}
Run Code Online (Sandbox Code Playgroud)

但在这两种情况下都会抛出异常(在几乎完全相同的行中,出于调试目的).唯一的区别是类型.

问题:在具有单个引用类型参数的公共方法上,如果我要立即使用该参数,我是否仍应检查它是否是?null

c# exception argumentnullexception nullreferenceexception

8
推荐指数
1
解决办法
1202
查看次数