Cause of "system. nullreferenceexception: object reference is not set to the instance of the object.

Source: Internet
Author: User

From the network. Collect and organize data.

Today, I encountered "the instance that has not set the object reference to the object ",CodeMore or less have encountered any change exceptions. If you encounter a simple problem, collect the following information on the Network:

1,ViewstateThe object isNull.
2 ,DatesetNull.
3 ,SQLStatement orDatebaseCauseDatareaderNull.
4 When the string variable is declared without a null value, the variable is applied.
5 UnusedNewInitialize the object.
6 ,SessionThe object is empty.
7 When assigning text values to controls,The value does not exist.
8 , UseRequest. querystring ()Hour,The retrieved object does not exist.,Or if the value is null, the initial value is not assigned.
9 , UseFindcontrolHour,The control does not exist but is not preprocessed.
10. Repeated definitions may result in an error in setting the object reference to an instance.

Example 1:

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.

 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 ( '   '  );} 

 

Example 2:

Binding, Eval ("field"), this method is more common, in some cases to convert string comparison

<% # 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.

Therefore, the Preventive statement is as follows:

<% # Convert. tostring (eval ("field") = "1 "? "Yes": "no" %>

 

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.