Instance where the object reference is not set to the object (System. NullReferenceException)

Source: Internet
Author: User

The following is a basic explanation of this error:
1: essentially incorrect:
Copy codeThe Code is as follows:
Object a; // a is a Null object.
Protected void Page_Load (object sender, EventArgs e)
{
A. ToString (); // call the method of a Null Object
}

Of course! The result is as follows:

It is not easy to write such code so naked. It is usually more inclined to the following:

2: Common Errors:

Example 1: A function used to filter certain characters:
Copy codeThe Code is as follows:
Public static string FilterValue (string value)
{
String [] filterChar = new string [] {"\ '", ">", "<", "= ",";","\"", "--"};
For (int I = 0; I <filterChar. Length; I ++)
{
Value = value. Replace (filterChar [I], "");
}
Return value. Trim ('');
}

This function is easy to see: If the value is Null, It is equal to Null. Replace is called, and the above error occurs.
Therefore, the value is usually carried out in the first line of the function: if (! String. IsNullOrEmpty (value.

Example 2: General call errors, binding, and Eval ("field") are introduced. This method is common. In some cases, you need to convert it to a string for comparison. Here is an example:

<% # Eval ("field"). ToString () = "1 "? "Yes": "No" %>

When Eval ("field") is Null, a Null. ToString () will inevitably see the above error. What will happen?
1: The field value is Null.
2: Empty data rows, that is, no data in one row in your table, all of which are Null.
Copy code

Therefore, the Preventive statement is as follows:
<% # Convert. ToString (Eval ("field") = "1 "? "Yes": "No" %

Okay. Now you can understand what you understand in this article. If you have to say that you do not understand it, I have to praise you for your high IQ. Here is an introduction to IQ.

This exception occurs when a Null object calls a method (attribute or other member) and changes to Null. XXX.

Of course, in such an abnormal scenario, there are tens of thousands of records, but the essence is the same.

In my opinion, the reasons for this error vary according to the following:
Copy codeThe Code is as follows:
Newbird: I don't know this error, or the number of times I see this error is too small, so the code is basically not protected, like a pattern, and a large number of functions are lurking in this type of error killer.

I guess: the reason for not making any judgment on the code written by the new bird may be:
First, they do not know this situation. They have just learned and are not experienced enough to handle such exceptions.
Second: speculation is their high-profile belief that an extra Null judgment will lead to lower performance, and they are pursuing high performance. Therefore, basically, it is not added.



Zhongniao: I know this error, but I don't think much about it. I am not careful enough and I am not stable enough. I will write code, but it is generally not.

There must be more insects than new birds, so the reasons are certainly different.
I guess: the reason for Null reference in the code written by the Chinese bird may be:
Copy codeThe Code is as follows:
First, I did not develop my thinking habits. Under the pressure of working overtime, I wrote a function that was flushed and I came out. Sometimes I would add it. Generally, I did not add it, but I did not add it, when the error occurs, add it.

Second, the Chinese bird was in the high-performance R & D stage, and he liked to discuss performance issues with others, especially when the for frequency reached millions, when the Performance drops from 0.03 seconds to 0.01 seconds, it will improve the performance by three times. It is considered a heavyweight discovery and then recommended to the later users, and BS some beginners or similar birds that are not written in this way.
Likewise: When a function is added with a null judgment, it is highly considered to be millions of calls. If this judgment is called millions of times, isn't the performance greatly compromised?
If two judgments are added, that is, two million calls, that is, a considerable performance loss. How can this be accepted?
Therefore, if you do not add, add or not, and then add any errors.
Third, I am too lazy to understand this:
As we all know, basically all the code written by Chinese birds belongs to the object-oriented model, that is, every day and every object is involved, A lot of precision cannot afford to hurt!
Well, a function transmits a parameter and adds a judgment to you. There are not many codes, and there are only two rows without braces.
However, the function parameters basically written by the Chinese bird are three, five, five, six, and seven, and 80 or 90, which is quite confusing:
Add it. The workload is too large, and the performance is not high;
If you don't want to add it, it seems that there is no problem. If you think about it, you will be very cheerful, good, and add or not. If you do not get it wrong, add it.
In addition, it is necessary not only to make decisions on parameters, but also to implement the internal objects generated by functions. Too many objects should be considered.
We rely solely on our energy and consideration to add people with common sentiments. Therefore, most of the situations are: Add or not, and add errors!



Old bird: I am too familiar with this error and have a fine mind. The subconscious will take the initiative to defend against this error, but I am a bit confused. I should come back to avoid the first day, but I can't hide it for less than 15 years.

The old birds eat more insects, and after a hundred battles, they feel that the system is stable and happy.
I guess the reason for Null reference in the code written by laruence may be:
Copy codeThe Code is as follows:
1. When code is written too much, it basically depends on the subconscious reaction. That is to say, the potential is realized, and the white point is the habitual thinking.
Basically, this error will not be made, but it can be blocked by the subconscious. In addition, it depends on normal thinking.
The old bird is usually not very energetic. Sometimes it will go distracted. When it goes distracted, it will miss one. When it goes distracted, it will miss another, and it will be turned to God again, there is one more guest in the Peace room.
As a result, I try not to be distracted when writing code, but sometimes it will be missed.
Second, it is a fake old bird. The old bird is installed, but it is still a Chinese bird. It is hard to install it, but it will be installed, which indicates that the IQ is high.
The law of society indicates that the better the survival, the higher the installation level, the more the installation will be, the better the life will be, and the highest level of installation is the installation of grandchildren.
Sun Tzu is a historical figure: the s show that his IQ is really high, and there is no contempt for him. Because I sometimes study the S, I just can't go up with his IQ, so the realm has been unable to go up.


Next, I would like to add my personal views on the Chinese and old birds. The following points are only the hypothetical views played by the author and are irrelevant to the author himself:

Why does the Chinese Bird pursue performance?
Copy codeThe Code is as follows:
Speculation: because they usually only access part of the system and lack the understanding of the entire system, they want to write performance-oriented code in the region they are responsible, can this be wrong?
That's right, and it should be done in theory! However, stability is insufficient. If you can write stable and high-performance code, how good is it!


The focus is on the old bird: Why does the old bird not focus much on performance while seeking stability?

In fact, laruence does not focus on performance, but on:

I. stability. This is important:
Copy codeThe Code is as follows:
Because the system goes online:
First, you have to take responsibility for the boss. Do you say yes?
Then: if this product is intended to be presented to the customer, you must be responsible for the customer. Do you say yes?
Furthermore, if this product is going to be put online for operation, you should be responsible for the visitor. Do you say yes?
Therefore, no matter what your system is like, first of all, ensure stability. At least you should not make mistakes when demonstrating or operating on the boss, the customer, or the visitor. As for the slow speed, it's a good discussion and a good discussion.


Ii. Overall performance is better than local Performance
Copy codeThe Code is as follows:
I: This is very obvious. You write a good algorithm, but other players' databases are poorly written. Access is slow.
II: Therefore, we must ensure that the overall access performance is almost the same first, and then perform local optimization. This is in line with the current optimization thinking of the Chinese.
III.
IV: Also, after caching and static html are added as a whole, you will find that the maximum optimization code in the Local Area is basically useless, because access + return is directly used,
As for your logic code that maximizes performance, it's just a thousand years.


Of course, I am very responsible for this point of view. I absolutely have no intention of ignoring the maximization of Local performance. On the contrary, I have to encourage everyone to maximize local performance and try to write the best code:
Copy codeThe Code is as follows:
First, this is a necessary stage for every coders to go up. Skipping is not a good thing.
Second, let the boss get out of blood and balance the dissatisfaction of the employees: How good is it if you let me work overtime.


Major notes:
The vast majority of views in this article are the author's personal hypothetical views, which have nothing to do with me. I believe that the above views are somewhat one-sided and may be inconsistent with objective facts.
For the weekend, please take a look at the excitement and excitement. Open X!

This video ends here. You are welcome to watch it. We will try again next time. Thank you!

PS: Recently, the CYQ. Data V4.5 offline help document was released soon.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.