未设置对象引用。 [英] Object reference not set.

查看:77
本文介绍了未设置对象引用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我的系统上有很多时间发现错误

对象引用未设置为实例一个对象。



我知道它说空值。



但我想知道什么是确切的解决方案。

很多次我解决了一段时间,但我实际上无法彻底解决。

Hello,

I have many time found on my system that error comes as
"Object reference not set to an instance of an object".

I know it says that null value taken.

But i want to know what is exact solution for that.
It comes many times i resolved some time but i actully could not get thoroughly.

推荐答案





错误
Hi,

The error
Quote:

对象引用未设置为实例对象

"Object reference not set to an instance of an object"

将有以下原因:



1.)如果你有一个对象或变量; le已声明但未按提及初始化在上面的解决方案中。



2.)如果从任何方法或服务返回的对象没有任何值,并且您正在尝试检索某些值那么你也会得到这样的错误。



3 。)假设您已经从任何文件位置引用了配置值并且该文件不可用,那么您也会收到此类错误消息。



希望来自所有这些示例你可以清楚你为什么会遇到这样的错误。



要解决这类错误你需要在代码中进行空检查,你应该返回一些有意义的错误返回函数的错误消息并在UI中显示。



谢谢

Sisir Patro

will come with the following reasons.

1.) If you have an object or a variab;le which is declared but not initialized as mentioned in the above solution.

2.) If your object that is returning from any of the method or service is not having any values and you are trying to retrieve some values there then also you will get such error.

3.) Lets say you have refered a config value from any file location and the file is not available then also you will receieve such error message.

Hope from all these examples you can able to clear why you are having such errors.

To solve this kind of errors you need to have the null check in your code and you should return some meaningful error message to the returning function and display that in the UI.

Thanks
Sisir Patro


scenario1。以上所解释的所有注释

scenario2:这也是代码背后的问题,如果你没有在aspx页面中创建控件,但是尝试分配值也会出现同样的错误。
scenario1. all explained above comments
scenario2:this, comes in code behind also, if u without creating controls in aspx page, but trying to assign value to also,the same error will come.


你需要初始化对象。

当你不使用关键字new时会发生这种情况



示例你有一个Car类,你想在代码中的某个地方使用它。



You need to initialize the object.
It happens when you do not use the keyword "new"

Example You have a class "Car" and you want to use it somewhere in the code.

public Class Car{
  public Color CarColor{ set; get; }
  public int TopSpeed { set; get; }
  public string Make { set; get; }
  public string Model { set; get; } 
}





导致这样的例外:



Causing the exception like this:

Car car;
car.TopSpeed = 250;  //nullreference exception here





避免这样的删除:



Avoiding the excpetion like this:

//to avoid nullreference exception
Car car = new Car();
car.TopSpeed = 250;  //no error thrown the topspeed property is set to 250.





使用new关键字将使程序保留内存,就像它所在的所有变量/函数一样。在这种情况下,应用程序将保留内存以保存CarColor,TopSpeed,Make和Model。



如果您想要另一辆车,请务必再次使用new关键字:



using the "new" keyword will make the program, reserve memory, as it where, for all the variables/functions that that class needs. In this case the application will reserve memory to hold CarColor, TopSpeed, Make and Model.

If you want another car, make sure to use the "new" keyword again:

Car car2 = new Car();





否则你再次获得例外。



希望这会有所帮助。



otherwise you get the exception again.

Hope this helps.


这篇关于未设置对象引用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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