Analysis of several common mistakes JAVA
1, null pointer wrong java.lang.NullPointerException
JAVA use basic data types, variables is the value of either the default value, if not their normal assignment, it can not be compiled procedures, the use of basic data types JAVA (double, float, boolean, char, int, long) normally does not cause abnormal null pointer. Evidently, null pointer with abnormal main targets of the operation with relevant.
Below listed first indicators of possible air abnormal situations and the corresponding solutions:
No matter whether the object is empty directly into use.
(JSP) code of 1:
Out.println (request.getParameter ( "username"));
Description:
A code of the function is very simple, user input is the output table domain "username" value.
Description:
It appears that the above statement not find any grammatical mistakes, and in most cases also Oubudao any problem. However, if a user in the input data does not provide any form fields "username" value, or through some form of direct input bypassing the way, at this time request.getParameter ( "username") of the value of air ( is not empty string is empty object null.), out println object is not directly target the air operation, paragraph 1 of the code of JSP pages will be dished out the "java.lang.NullPointerException" abnormal.
Even if the object may be empty, it calls java.lang.Object Object object itself or some of the ways such as toString (), equals (Object obj) operation.
(JSP) of the Code 2:
String userName = request.getParameter ( "username");
If (userName.equals ( "root"))
{….}
Description:
2 code of the function is to provide users of the user name, if the user name as "root" user, on the implementation of some special operation.
Description:
In paragraph 2 of the code, if a user does not provide the form domain "username" value, as null string userName target value can not be the object of a null object direct comparison with another, equally, paragraph 2 of the code JSP pages will be dished out (java.lang.NullPointerException) null pointer wrong.
(JSP) code of 3:
String userName = session.getAttribute ( "session.username"). ToString ();
Description:
3 of the code is the function of the value of session in session.username removed, and assign the value of the string userName object.
Description:
Under normal circumstances, if a user's session has been carried out, it will not appear any problem, but if this application server restart, and users have not re-register, (and possibly users to disable the browser, but still open the original pages.) then, the value of this session will be the failure, resulting in the session session.username value is empty. On the subject of a null for the direct implementation of toString () operation, the system will lead dished out (java.lang.NullPointerException) null pointer anomaly.
Solutions:
In order to ensure that the operation or use of non-space object, if we have to operate on a certain object or cited, we have to check whether the object is instantiated without air; in the system and add space-time targets for the processing of information .
Such as: String object to the preservation of users is the result of the object involved in the operation if, first testing whether it is empty, check to the object is empty, it can choose either to the following address:
Approach 1) to check for air targets, set target values for the empty string or a default value;
Handling 2) detected object is empty, the implementation of a certain operation is not directly Jump to the other processing.
Approach 3) to check for air targets, prompts the user to operate the mistakes.
2 of the code will be conducted in accordance with the above rewritten by:
Mode 1:
String userName = request.getParameter ( "username");
/ / Value of the space-time variables into a default empty string
If (userName == null)
UserName = "";
If (userName.equals ( "root"))
{……….}
Mode 2:
String userName = request.getParameter ( "username");
/ / Value of the space-time variables into a default empty string, and not run the operation.
If (usreName! = Null)
(
If (userName.equals ( "root"))
{……….}
)
Mode 3:
String userName = request.getParameter ( "username");
/ / Value of the space-time variables into a default empty string, and not run the operation.
If (usreName == null)
(
/ / Prompts the user to input information is empty
)
Practice, the above three approaches to the provision also applies to other unusual treatment:
Exceptions mode 1) to check anomalies, and set target values for the empty string or a default value;
Exception Handling method 2) to detect anomalies, there is no implementation of a certain operation, Jump directly to other processing.
Exceptions mode 3) to check anomalies, and prompts the user to operate the mistakes.
2, the format of digital error java.lang.NumberFormatException
(JSP) code of 3:
String s_memberid = request.getParameter ( "memberid");
Int i_memberid = Integer.parseInt (s_memberid);
Description:
Of the code above is the role of the form will be submitted by the user domain memberid into integer values.
Description:
If you enter the correct figures such as: 1082, there will be no problem. However, if the user input T1082, T1082 is not legitimate because the digital format, JAVA and can not be converted into the right figures, dished out java.lang.NumberFormatException lead to abnormal digital format.
Solutions:
Used in any string into figures, capture anomalies, the unusual circumstances handled by an unusual approach: to check abnormal, that is, assign a default value of a variable (in some cases may lead to an other procedures wrong [for example, did not deal with other modules give you the default value of circumstances that might lead to some unusual or error has occurred.]) by three unusual approach: to check abnormal, prompt the user to use the correct digital format input. (Achieve slightly trouble, but you will be wrong block in the module before [that you provide to the value of other modules are safe.) By this method of rewriting procedures in the programming is a little trouble, but You modules will indeed be more robust. 3 code of conduct in accordance with the above requirements rewritten by:
String s_memberid = request.getParameter ( "memberid");
Int i_memberid;
Try
(
I_memberid = Integer.parseInt (s_memberid);
…
)
Catch (NumberFormatException nfe)
(
/ / Mode 1: (simple, direct to the code for a default value 0;)
I_memberid = 0;
/ / 2 means: (very primitive practices, recommend the use of more friendly manner Tips)
Out.println ( "");
)
3, string, such as cross-border java.lang.StringIndexOutOfBoundsException string of errors related error
Code segment 4:
String s_all_power = "1010011";
String s_access_power s_all_power.substring = (3,4);
Description:
Function over the functions of the code is to obtain s_all_power string in the first four characters.
Description:
Under normal circumstances, the process will not be a problem, if for some reason s_all_power length shorter, procedures will be dished out string error.
Solutions: interception of the string (substring, charAt), converted to byte array (getBytes), an array of characters converted to a string (valueOf) operation, the first string object in the operation of the existence of (whether the air ) and the length of examination, and then to operate.
Be rewritten:
String s_all_power = "1010011";
If (s_all_power.length> 4)
String s_access_power s_all_power.substring = (3,4);
4. Class definition not found error java.lang.NoClassDefFoundError
Reasons:
As the procedure to call the document does not correct category JAVA 3.5;
Solutions:
JAVA class files will be re-upload.
JAVA type document has been uploaded, but the application server has not detected, it is proposed to re-JSP pages updated.
Solutions:
JSP pages will be updated and uploaded or application servers will be restarted.
5, JAVA wrong java.lang.Error
Reasons:
1. System access to external resources, the implementation of the closure operation, resulting in wasting a lot of external resources, and could eventually lead to the denial of normal system operation;
2. System of external resources to close too many times, unable to deal with external systems;
3. System access external resources unusual circumstances.
Solutions:
1. External resources before the visit, the first to examine the resources (such as databases) can connect or normal operation.
2. Access to external resources, if the link to shut down certain operations, and only conduct a closed operation.
3. In the same operation as far as possible in the share of external resources to reduce the consumption of resources operations and improve the efficiency of the implementation process.






