JAVA several common mistakes Brief http://www.chinaunix.net Author: jsf008 Posted: 2003-10-24 08:05:57
1. Empty pointer error java.lang.nullPointerException uses basic Java data types, the value of the variable is either a default value, and if there is no normal assignment, the program cannot be compiled, so the basic Java data type (Double, Float) , Boolean, CHAR, INT, and long do not generally cause an empty pointer anomaly. It can be seen that the empty pointer is mainly related to the operation of the object. Let's list several situations and corresponding solutions that may have an empty pointer: whether or not the object is empty. (Jsp) code segment 1: Out.println (Request.getParameter ("UserName")); Description: The function of the code segment 1 is very simple, that is, the value of the output user input, "UserName". Description: It seems that the above statement can't find something wrong, and in most cases, there is no problem. However, if a user does not provide a value of the form field "username" when entering the data, or bypass the form directly input, the value of Request.GetParameter ("UserName") is empty (not empty) String, is empty object null.), The PrintLn method of the OUT object is unable to move directly to the empty object, so the JSP page of the code segment 1 will throw "java.lang.nullpointRexception" exception. Even if the object may be empty, some methods of java.lang.object or Object object itself are also called to Tostring (), Equals (Object Obj). (Jsp) code segment 2: String username = request.getParameter ("username"); if (username.equals ("root")) {....} Description: The function of code segment 2 is the username provided by the user. If it is a user name "root", some special operations are performed. Description: In the code segment 2, if the user does not provide a value of the form field "username", the string object username is a null value, which cannot be directly compared to another object, the same, the code segment 2 JSP page will throw (java.lang.nullpointerException) empty pointer error. (Jsp) code segment 3: String username = session.getattribute ("session.username"). TOSTRING (); Description: The function of code segment 3 is to take the value of session.username in the session and assign the value to the character. String object Username. Description: In general, if the user has already made a session, if there is any problem, if the application server restarts, the user has not logged in, (may also be the user to close the browser, but Still open the original page.) So, at this time the value of the session will fail while causing the value of session.username in the session. For a direct execution of a direct execution of a NULL, it will cause the system to throw an empty pointer anomaly.
Solution: To ensure operation or reference objects are not empty, if we want to operate or reference an object, we first check if the object is instantiated and not empty; and adding to an object in the system The processing of the situation. Such as: String object saves the result of the user's submission; if it is involved in the operation of the object, first detect if it is empty, check the object to be empty, and then select any of the following processing mode: processing mode 1) check When the object is empty, the setting object value is an empty string or a default value; how to process mode 2) When the object is empty, do not perform an operation, directly jump to other processing. Processing 3) When the object is empty, it is prompted that the user operates an error. Rewriting the code segment 2 in the above manner, resulting: mode 1: string username = request.getParameter ("username"); / / This variable is empty, converted to the default empty string IF (username == null) Username = ""; UserName.Equals ("root")) {........} mode 2: String username = request.getParameter ("username"); // This variable is empty Convert to the default empty string, do not perform the relevant operations. IF (USRename! = Null) {if (UserName.Equals ("root")) {........}} method 3: string username = request.getParameter ("username"); // When the variable is empty, convert to the default empty string, do not perform the relevant operations. If (usrename == null) {// prompt user input information is empty} In practice, the above is provided to three processing methods also apply to other exception processing: Exception processing mode 1) Check to abnormality, set object value Empty string or a default value; exception handling mode 2) detecting an abnormality, do not perform an action, directly jump to other processing. Abnormal processing mode 3) Check the abnormality, prompting the user to operate an error. 2. Format Number Error Java.lang.NumberFormATException Analysis (JSP) Code Segment 3: String S_meMberid = Request.getParameter ("MemberID"); int i_memberid = integer.parseint (s_memberid); Description: The above code segment is to submit the user The value of the formal field MEMBERID is transformed into an integer. Description: If the user enters the correct number such as: 1082, there will be no problem. However, if the user inputs T1082, Java cannot convert it into a suitable number because T1082 is not a legal digital format, causing the java.lang.NumberFormatexception digital formatting exception. Solution: When any string is converted to a number, capture an abnormality, process the abnormal situation Press Outvention Press: Check the abnormality, that is, assign a variable to a variable; (may result in some cases Other programs errors [Compared with the other modules, there is no default case you give to you, it may cause some exceptions or errors.
]) Press the abnormality processing mode 3: Check the abnormality, prompting the user to enter the correct digital format. (Realize a little more trouble, but block the error before your module [that is, you provide the value to other modules].) This method is rewritten on the program, which is slightly troublesome when programming, but this is indeed Will your module is more robust. Rewriting the code segment 3 by: string s_meterid = request.getParameter ("memberid"); int i_memberid; try {i_memberid = integer.parseint (s_memberid); ...} catch (NumberFormatexception NFE) {// Method 1: (Simple, directly to this number is a default value 0;) i_memberid = 0; // mode 2: (very simple practice, it is recommended to use more friendly prompts) Out.println ("