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" %>