Object reference not set to an instance of an object

Source: Internet
Author: User

1 Nature of the error:

Object a;//a is null
protected void Page_Load (object sender, EventArgs e)
{
A.tostring ();//method that invokes a null object
}

The result is as follows:

It's not easy to write this kind of code in such a stark way, usually more inclined to the following:

2: Error of the usual nature:

Example 1: A function that filters certain characters:

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 being called, and the above error occurs.

Therefore, usually, in the first line of the function, the value will be: if (!string. IsNullOrEmpty (value)) a moment.

Example 2: Again, a generic invocation error, binding, Eval ("field"), this method is more common, some cases to go to the string comparison, here is an example:

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

When eval ("field") is null, a null.tostring () will inevitably appear with the error above, what happens?

1: The value of the field is null
2: Empty data row, that is, your table row of data are not, all is null.

So the precautionary wording is:

<%# convert.tostring (Eval ("field")) = = "1"? " Yes ":" No "%

See this exception: A null object invokes a method (property or other member) into a null.xxx throw. of Course, the scene of this anomaly, that is thousands, the number is not complete, but the essence is the same.

Object reference not set to an instance of an object

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.