什么是的NullReferenceException的含义 [英] What is the meaning of NullReferenceException

查看:688
本文介绍了什么是的NullReferenceException的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
什么是一个NullReferenceException .NET?

例如, System.NullReferenceException 是未处理,与消息对象引用不设置到对象的实例。

For example, "System.NullReferenceException was unhandled", with Message "Object reference not set to an instance of an object."

这是什么异常的含义,以及它如何解决?

What is the meaning of this exception, and how can it be solved?

推荐答案

这意味着你已经试过访问的一些成员是不是有:

It means you've tried to access a member of something that isn't there:

string s = null;
int i = s.Length; // boom



刚刚修复为空的东西。要么让非空,或先执行零测试。

Just fix the thing that is null. Either make it non-null, or perform a null-test first.

还有一个的极端情况的位置有关可空< T> ,泛型和通用约束 - (!不过,赫克说,我打了这个问题)。有点不太可能,虽然

There is also a corner-case here related to Nullable<T>, generics and the new generic constraint - a bit unlikely though (but heck, I hit this issue!).

这篇关于什么是的NullReferenceException的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆