如何解决这种错误?请帮忙! [英] How to solve this kind of error? Please help!

查看:67
本文介绍了如何解决这种错误?请帮忙!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 System.Web.HttpUnhandledException: 类型' < span class =code-comment>抛出了System.Web.HttpUnhandledException'。 ---> System.NullReferenceException:对象引用未设置为对象的实例。 

解决方案

您可以防止出现此类错误通过从不假设一个对象在使用它之前已经被正确初始化(即,总是在使用任何类之前检查空值)。



在代码中,这是一个描述一般概念的小例子:

  //  这里myObject可以是任何引用类型变量 
if (myobject!= null ){
// 做点什么myObject
}
其他 {
// 显然存在问题,myObject不应为null,但它是。
// 在这种情况下你必须决定做什么。
}


System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.NullReferenceException: Object reference not set to an instance of an object.

解决方案

You can prevent this kind of error to happen by never assuming that an object has been correctly initialized before using it (i.e., always check for a null value before using any class).

In code, here's a small example describing the general idea:

// Here myObject can be any reference-type variable
if (myobject != null) {
   // Do something with myObject
}
else {
   // Obviously there is a problem, myObject should not be null, but it is.
   // You have to decide here what to do in this case.
}


这篇关于如何解决这种错误?请帮忙!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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