The following basic explanation of this error:
1: The nature of the error:
Copy Code code as follows:
Object a;//a are null objects
protected void Page_Load (object sender, EventArgs e)
{
A.tostring ();//Calling a Null object method
}
Of course! The results are as follows:
Writing this code in such a stark way is not easy, and usually prefers the following:
2: Error of the usual sex:
Example 1: A function that filters certain characters:
Copy Code code 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, for example, is easy to see: If value is passed in as null, it is equal to Null.replace is called, and the above error occurs.
So, normally, in the first line of a function, the value is performed: if (!string). IsNullOrEmpty (value)).
Example 2: Again, a generic invocation error, binding, Eval ("field"), which is common in some cases to a string comparison, here is an example:
<%# Eval ("field"). ToString () = = "1"? Yes ":" No "%>
When eval ("field") is null, a null.tostring () must also appear above the error, what happens?
1: The value of the field is null
2: Empty data rows, that is, you do not have a row of data, all null.
Copy Code
So the precautionary formulation is:
<%# convert.tostring (Eval (field)) = = "1"? Yes ":" No "%
Well, see this article whether understand or not understand, now should understand, if you have to say you do not understand, I have to praise you IQ high, below have IQ introduction, don't let go.
See this exception: A null object invokes a method (property or other member) that becomes null.xxx.
Of course, there is such an unusual scene, that is thousands, countless, but the essence is the same.
The reason for this error in three birds is, in the opinion of the individual, different, basically as follows:
Copy Code code as follows:
New bird: Do not know this error, or see this error too few times, so the code basic defense is not good, imitation, a large number of functions are lurking this wrong killer.
Personal guess: The code that the new bird writes is not judged for reasons that may be:
One: They do not know this situation, just learn, inexperienced, have not dealt with this abnormal experience.
Second: Speculation is their high-profile view: More than a null judgment, will make performance drop, they pursue high-performance, so basically, not add.
The bird: Know this mistake, just consider not much, the heart is not fine, people are not stable, write code will be added, but generally do not add.
The birds eat more worms than the new bird, so the reason for this is certainly different.
Personal guess: The code that the bird writes, the reason for the null reference may be:
Copy Code code as follows:
One: No habit of thinking, in the pressure of overtime, write a function is to brush out, occasionally add, generally do not add, plus or not, and so the error came out and added.
The second: In the period of high performance research and development period, the most like to discuss performance issues with others, especially when the for the number of times to reach millions, When the performance from 0.03 seconds down to 0.01 seconds, will be a full increase of 3 times times the performance of the Huan, and that this is a heavyweight discovery, and then recommended to the successor, and BS some not so write novice or similar birds.
Similarly: a function with a null judgment, to rise to the million call level of the height of consideration, if this judgment is called million, that performance is not a big loss?
If you add 2 judgments, that is the 2* million call, which is a considerable loss of performance, how is this acceptable?
Therefore, can not add, plus, and so the error came out and added.
Third: Too lazy, this oneself is so understanding said:
Everyone knows, in the birds write code, basically belong to object-oriented, it is every day and object in a piece of, each object to get to go, no amount of sperm can not hurt!
OK, a function to pass a parameter, give you a judge, the code is not much, not counting braces on two lines.
But the middle bird basically writes the function the parameter, only then 34,567,890, this lets the human tangle:
Add bar, a think, the workload is too big, and this performance feeling is not high;
Do not add it, it seems that there is no problem, so think Ah, the eyes broad but cheerful, good, add not add, the error came out again.
There is not only the parameters of the judgment to toss, the function generated within the object, but also to engage in, too many objects to consider.
Light depends on energy and consideration, add a little human nature, so most of the situation is happening: Add not add, error out and then add!
Veteran: Too familiar with this mistake, the heart is fine enough, write code subconscious will actively add defense, but a sparse, the coming or will come, avoid the first grade, hide but 15.
Old birds eat more worms, and veteran veterans of the battle, that the system is stable, is happy.
Personal guess: The code that the veteran wrote, the reason for the null reference may be:
Copy Code code as follows:
One: code written more, basically rely on subconscious response, that is, the potential to play, and then the white point is habitual thinking.
So basically will not how to make this mistake, but by the subconscious, the basic can block, basically, but also rely on normal thinking.
Veteran usually not very good, occasionally will be distracted, a mind-wandering, leakage of one, and then a wandering, and missed one, again a wandering, was God called, the morgue more than a guest.
So I write code, try not to wander, avoid being called, but occasionally also leak.
Second: is false veteran, veteran is installed, in fact, or in the bird, hard to install, but will be installed, indicating high IQ.
The law of society shows that the better the survival, the higher the degree of installation, the more will be installed, the better life, loaded to the highest level, that is to install grandson.
Grandson is a historical figure: will be 36 meters, the description of the grandson of the IQ is really high, there is absolutely no contempt for the idea, because 36 meters sometimes I am also learning, but IQ has been not to go, so the realm has been on not to go.
To add to this, personal opinion of the veteran, the following view is only for the author's personal conjecture, and the author has nothing to do:
Why does the bird pursue performance?
Copy Code code as follows:
Conjecture: Because they usually touch only a part of the system and lack the understanding of the system as a whole, do they want to write the most performance-oriented code in the area they are in charge of, is that a mistake?
Yes, and theoretically it should be done! However, the stability is insufficient, if can write the stable and the high performance code, how good!
The emphasis is still on old birds: Why are the old birds not paying much attention to performance and seeking stability?
In fact, the veteran is not not concerned about performance, but his concern is:
One: Stability, this is important:
Copy Code code as follows:
Because the System one on-line:
First of all: be responsible to the boss, you say?
Then: If this product is to be demonstrated to the customer, it is responsible for the customer ah, you say not?
Furthermore: if the product is to be operated on line, it must be responsible for the visitors, don't you think?
So, no matter how your system, first of all, to ensure stability, at least to the boss or customers or visitors to demonstrate or operate, you can not make mistakes, as slow, not slow, that good discussion, good consultation.
Second: The overall performance is greater than the local performance
Copy Code code as follows:
I: This is very obvious, you write a very good algorithm, but the other players database write poor, a visit, very slow, this how to say the past.
II: So to ensure the overall access performance is almost first, and then the local optimization, which is more in line with the Chinese current optimization thinking ah.
III: Besides, everyone has maximized local performance, a visit, or slow, can not find the optimization of the place, this is going to be an accident: the boss had to bleed to buy hardware.
IV: Also, as a whole added cache + static HTML, you will find that the local maximum optimization code, basically is not useful, because the direct access + return,
As for the logical code that maximizes your performance, that's the Thousand years walk.
Of course, the individual is very responsible for this view, there is absolutely no contempt for the meaning of local performance maximization, on the contrary, we have to encourage everyone to maximize local performance, and strive to write the best code:
Copy Code code as follows:
One: This is a step by step for every yard farmer, skipping is not a good thing.
Secondly: more let the boss out of blood, you can balance employee dissatisfaction mood: You let me work overtime, I let you bleed, how good it.
Important NOTE:
The majority of the views of this article on the author's personal conjecture, and I have nothing to do, I think that the above views are one-sided, may be inconsistent with the objective facts.
Please look at the weekend for all of you, less a radical, more than a thrill, open x!
This video ends here, welcome to watch, next time, thank you!
PS: Recently on the way to toss the next cyq. Data V4.5 offline Help documentation, released soon, stay tuned.