When writing Java programs, especially JSP/Servlet and other things, there is often such a situation: of course, when you have worked hard to lay down thousands of lines of letters, write the last braces; when you run this program for debugging, The nullpointerexception message suddenly appears. Are you very disappointed. In the past, I would have picked up the cup on the table.
After more than a year of playing letters, I gradually summed up some experiences about the villain nullpointerexception.
The nullpointerexception exception occurs when the object does not exist but is not captured for processing. However, in Java, It is thrown by most of the broken parts, so it often appears inadvertently in front of you. The original description in the API is as follows: thrown when an application attempts to use null in a case where an object is required. These include:
* Calling the instance method of a null object.
* Accessing or modifying the field of a null object.
* Taking the length of null as if it were an array.
* Accessing or modifying the slots of null as if it were an array.
* Throwing null as if it were a throwable value.
Applications shocould throw instances of this class to indicate other illegal uses of the null object.
According to my own statistics, the most frequently occurring class in my code is string. so let's take string as an example to see how to avoid nullpointerexception when using this class.
From the source code of Java, the string class throws the nullpointerexception exception in two places. One is a string constructor and the other is the tolowercase method, therefore, it is best to catch exceptions when using these two methods.
We often encounter the equals method. The nullpointerexception of equals is caused by the nonexistent object. For example, str. Equals ("this is a string.") and STR = NULL. Then nullpointerexception occurs. How can this problem be solved? It is to swap the positions of constants and variables, "This is a string.". Equals (STR), so that nullpointerexception will not be seen again.
In JSP, some forms are often submitted to the server, but sometimes the form items are not filled in, so when the JSP program processes the uploaded data, there will be exceptions. In this case, you must make a judgment before using the data, for example, request. getparameter ("str"). If you do not have any processing, you may encounter an error.
String TMP = "";
If (request. getparameter ("str ")! = NULL)
TMP = request. getparameter ("str ");
Then you can perform operations on TMP. Of course, almost all nullpointerexception problems can be solved using this method.
In addition, when using JDBC, The resultset object will often encounter nullpointerexception exceptions. Generally, this problem occurs in the resultset mainly because of SQL errors.
Now let's think about this. You are welcome to add, criticize, and correct your ideas. Abbreviation: String TMP = request. getparameter ("str") = NULL? "Defaultstring": request. getparameter ("str ");