I think the most common error during development is a null pointer.
I have been working for a while and have encountered many null pointers.
List list = dao. findById (id );
For (int I = 0; I <list. size (); I ++ ){
// Do some thing
}
If list = null, the use of the null pointer exceptionjava pointer array will appear.
The reason is not that List list = null but that of list. size (). If list is null, the size method cannot be used, so a null pointer error occurs.
In many cases, null pointer errors are similar to the above. Of course, they may often occur in multi-layer calls.
Therefore, it is generally necessary to process the return value.
List list = dao. findById (id );
If (list = null ){
// Generally, return processing is performed here to avoid the following statement from calling null. A null pointer occurs.
// Of course, you can also use the if else statement to skip statements that may contain null pointers.
}
If (list! = Null & list. size () <1 ){
// It must be noted that the content is empty and the content is null. if the content is empty, no null pointer is reported.
// But the for loop below is obviously not executed
}
For (int I = 0; I <list. size (); I ++ ){
// Do some thing
}
In addition, the empty pointer to the jsp page is the same, because jsp is compiled into a java class to run.
For example, the following code
<%
String para = ""
List list = request. getAttribute ("users ");
Log.info (para. length (); // output 0
// If list is null, a null pointer will also appear below
For (Iterator I = list. iterator (); I. hasNext ();){
// Output
}
%>
Excerpted from: a blog with a wall and grass