首页
学习
活动
专区
圈层
工具
发布
    • 综合排序
    • 最热优先
    • 最新优先
    时间不限
  • 来自专栏walterlv - 吕毅的博客

    NullReferenceException,就不应该存在!

    NullReferenceException,就不应该存在! 2017-11-29 16:08 如果要你说出 .NET 中的三个异常,NullReferenceException 一定会成为其中一个;如果说出 .NET 中的一个异常,NullReferenceException ---- NullReferenceException 的可恨之处 你说 NullReferenceException 可以告诉你程序中某个字段为 null,告诉你程序发生了 BUG。 这是因为 NullReferenceException 异常除了调用栈(StackTrace)之外不能提供其他额外的异常信息,连变量或字段名都不能提供。 NullReferenceException 的替代方案 既然 NullReferenceException 没能给我们提供足够的信息,那么我们就自己来提供这些信息。

    1.5K10发布于 2018-09-18
  • 来自专栏JusterZhu

    自动更新程序GeneralUpdate

    { if (args.Length == 0) { throw new NullReferenceException } if (string.IsNullOrWhiteSpace(PacketName)) { throw new NullReferenceException } if (string.IsNullOrWhiteSpace(DownloadPath)) { throw new NullReferenceException } if (string.IsNullOrWhiteSpace(InstallPath)) { throw new NullReferenceException } if (string.IsNullOrWhiteSpace(MD5)) { throw new NullReferenceException

    1.7K20编辑于 2022-12-07
  • 来自专栏DotNet NB && CloudNative

    C# 如何实现一个事件总线?

    servicesProvider.GetService<IAsyncEventHandler<TEvent>>(); if (handler is null) { throw new NullReferenceException serviceProvider.GetService<IAsyncEventHandler<TEvent>>(); if (handler == null) throw new NullReferenceException serviceProvider.GetService<LocalEventBusManager<TEvent>>(); if (manager is null) throw new NullReferenceException serviceProvider.GetService<LocalEventBusManager<TEvent>>(); if (manager is null) throw new NullReferenceException serviceProvider.GetService<LocalEventBusManager<TEvent>>(); if (manager is null) throw new NullReferenceException

    47510编辑于 2024-04-15
  • 来自专栏喵叔's 专栏

    正确调用事件处理程序

    (this); } } 上面的代码中存在一个严重的问题,当在对象上触发 demo 事件时并没有关联的事件处理程序的话,C# 将会用 null 值来表示没有处理程序与该事件相关联,进而将会引发 NullReferenceException =null)时发现 demo 不等于 null ,正巧这时线程 B 将唯一的事件处理程序解除了订阅,这时线程 A 再调用 demo 时事件处理程序已经变为了 null ,进而导致 NullReferenceException 上述这种方法是对等号右侧的内容进行了浅拷贝创建了新的引用,使其指向原来的事件处理程序(相当于给事件订阅者生成了一个快照),当另一个进程注销掉事件处理程序时,注销的只是 demo 上所绑定的处理程序,因此当当前的线程执行 handler 时是不会出现 NullReferenceException

    1.5K10发布于 2020-09-08
  • 来自专栏DotNet NB && CloudNative

    C# 可为空引用类型

    导致可为空引用类型不尽理想的一些原因在于: 对空值调用成员会导致 System.NullReferenceException 异常抛出,导致生产代码抛出 System.NullReferenceException 减少 NullReferenceException 抛出:降低 NullReferenceException 异常抛出的可能性,具体是通过改进静态流分析,标记出可能存在问题的情况,即调用值成员之一前未显式检查值是否为空 启用静态流分析警告抑制:支持某种形式的“相信我,我是程序员”声明,方便开发人员重写编译器的静态流分析,从而抑制任何可能的 NullReferenceException 警告。 这很遗憾,但支持可为空引用类型应该会降低 NullReferenceException 抛出可能性,尽管不是完全杜绝。(这类同于分配变量时的编译器检查易错性。) 久而久之,更新后的可为空代码和旧代码之间的阻抗不匹配将会消失,同时减少了过去常常出现的 NullReferenceException bug。

    1.5K20编辑于 2023-08-29
  • 来自专栏DotNet NB && CloudNative

    C# 如何实现一个事件总线

    servicesProvider.GetService<IAsyncEventHandler<TEvent>>(); if (handler is null) { throw new NullReferenceException serviceProvider.GetService<IAsyncEventHandler<TEvent>>(); if (handler == null) throw new NullReferenceException serviceProvider.GetService<LocalEventBusManager<TEvent>>(); if (manager is null) throw new NullReferenceException serviceProvider.GetService<LocalEventBusManager<TEvent>>(); if (manager is null) throw new NullReferenceException serviceProvider.GetService<LocalEventBusManager<TEvent>>(); if (manager is null) throw new NullReferenceException

    60210编辑于 2024-01-17
  • 来自专栏全栈程序员必看

    未将对象引用设置到对象的实例–可能出现的问题总结

    异常具体信息: System.NullReferenceException: 未将对象引用设置到对象的实例。 源错误: 。。。。 四、 一般出现NullReferenceException异常的我个人总结有下面情况: 1、对象所在的命名空间没有引用 2、对象没有实例化 3、出现异常,实例化失败对象为 null 五、 IIS 扛不住的时候 可是当执行的时候,就出现” NullReferenceException: 未将对象引用设置到对象的实例“。结果就因它而搞了几个小时。

    4.4K30编辑于 2021-12-23
  • 来自专栏全栈程序员必看

    未将对象引用设置到对象的实例–可能出现的问题总结

    异常具体信息: System.NullReferenceException: 未将对象引用设置到对象的实例。 源错误: 。。。。 四、 一般出现NullReferenceException异常的我个人总结有下面情况: 1、对象所在的命名空间没有引用 2、对象没有实例化 3、出现异常,实例化失败对象为 null 五、 IIS 扛不住的时候 可是当执行的时候,就出现” NullReferenceException: 未将对象引用设置到对象的实例“。结果就因它而搞了几个小时。

    5.9K10发布于 2021-11-24
  • 来自专栏博客园

    c#值类型与引用类型区别

    试图使用null引用类型变量会抛出NullReferenceException异常。相反,值类型的变量总是包含其基础类型的一个值,而且值类型的所有成员都初始化为0。 值类型变量不是指针,访问值类型不可能抛出NullReferenceException异常。CLR确实允许为值类型添加“可空”(nullability)标志。

    1K20发布于 2019-02-25
  • 来自专栏黄腾霄的博客

    2019-3-8-resharper喊你回家判空啦

    NullReferenceException一定是大多数程序员最讨厌的异常之一 ---- 我们在书写代码时看到NullReferenceException一定很崩溃,但是更崩溃的事情是在团队协作时,小伙伴写的方法给你返回了一个

    63120发布于 2020-06-10
  • 来自专栏walterlv - 吕毅的博客

    WPF 支持的多线程 UI 并不是线程安全的

    Framework 4.7.2 版本的 WPF,还是 .NET Core 3 版本的 WPF 都会出现此问题 现象: 抛出异常,程序崩溃 比如下面是其中一种异常: Exception thrown: 'System.NullReferenceException System.NullReferenceException: Object reference not set to an instance of an object.

    98320编辑于 2023-10-21
  • 来自专栏林德熙的博客

    dotnet 6 已知问题 ManualResetEventSlim 的 Set 方法抛出空异常

    当前这个问题在 .NET 7 修复 这是从我的埋点上报遥测收集到的异常信息,在线程池里面的调用堆栈,调用到 ManualResetEventSlim 的 Set 方法,抛出了 System.NullReferenceException 异常,堆栈如下 System.NullReferenceException: Object reference not set to an instance of an object.

    46720编辑于 2023-07-01
  • 来自专栏walterlv - 吕毅的博客

    WPF's multi-threaded UI is not thread safe

    application crashes For example, the following is one of the exceptions: Exception thrown: 'System.NullReferenceException System.NullReferenceException: Object reference not set to an instance of an object.

    72720发布于 2020-02-10
  • 来自专栏C#

    C#常用的IO操作方法

    { if (fromDir == null || toDir == null) { throw new NullReferenceException DeleteDir(DirectoryInfo dir) { if (dir == null) { throw new NullReferenceException { if (dir == null || dir.Trim() == "") { throw new NullReferenceException

    1.1K111发布于 2018-01-04
  • 来自专栏Web开发之路

    C# 学习笔记(13)—— 扩展方法

    从编译器发现扩展方法的过程来看,方法调用的优先级顺序为: 1、类型的实例方法 2、当前命名空间下的扩展方法 3、导入命名空间的扩展方法 空引用也可调用扩展方法 在 C# 中,在空引用(即 null)上调用实例方法会引发 NullReferenceException this object obj) { return obj == null; } } } 以上代码在空引用上去调用扩展方法确实没有出现 NullReferenceException

    62330编辑于 2023-10-20
  • 来自专栏草根专栏

    .NET Core 网络数据采集 -- 使用AngleSharp做html解析

    比如说你想要找的标签不存在, 那么就会返回null, 然后再调用改标签的属性, 就会发生NullReferenceException. 所以这种情况可以捕获NullReferenceException, 也可以使用代码判断: public static async Task ReadNonExistTagAsync() Message :{0} ", e.Message); return null; } catch (NullReferenceException ) { Console.WriteLine($"{nameof(NullReferenceException)}:"); Attributes["href"].Value; Console.WriteLine(alink); } catch (NullReferenceException

    4.8K00发布于 2018-05-30
  • 来自专栏张善友的专栏

    .NET不可变集合已经正式发布

    因此,AddRange方法的实现会因为NullReferenceException的产生而崩溃。 由于这种接口引用是非空的,使用者在调用它的方法或者属性时不会考虑到有可能产生NullReferenceException

    1K100发布于 2018-01-29
  • 来自专栏草根专栏

    Nullable Reference Types 可空引用类型

    但如果想避免NullReferenceException的发生,确实需要做很多麻烦的工作。 可空引用类型 Null Reference Type 所以,C# 8的可空引用类型就出现了。 这个叹号的作用仅仅是去掉了警告,它没有改变任何运行时的状况,如果Address为null,那么这句话仍然会抛出NullReferenceException

    1.1K30发布于 2019-10-15
  • 来自专栏DotNet NB && CloudNative

    基础:C# try catch finally异常处理(Exception)

    MissingMemberException 访问一个无效版本的DLL NotFiniteNumberException 对象不是一个有效的成员 NotSupportedException 调用的方法在类中没有实现 NullReferenceException NullReferenceException 当一个空对象被引用时运行时引发。 InvalidOperationException 当对方法的调用对对象的当前状态无效时,由某些方法引发。

    90210编辑于 2024-04-15
  • 来自专栏张善友的专栏

    Quartz.NET 1.0正式发布

    scheduler [QRTZNET-131] - NthIncludeDayTrigger doesn't utilize custom TimeZone correctly [QRTZNET-132] - NullReferenceException

    68380发布于 2018-01-30
领券