Analysis of Java. Lang. nullpointerexception

Source: Internet
Author: User
Analysis of Java NULL pointer exceptions (1)

The Java language has an "exception mechanism" to handle special cases in the program. For example, if the divisor is zero, it is an "exception ".

The NULL pointer exception java. Lang. nullpointerexception is a common exception. Let's talk about its analysis method.

When the basic java data type is used, the value of the variable is either the default value. If no value is assigned to the variable, the program cannot be compiled. Therefore, the basic java data type (double, float, boolean, Char, Int, long) generally does not cause NULL pointer exceptions. It can be seen that Null Pointer exceptions are mainly related to object operations.

The following lists possible NULL pointer exceptions. Let's take a look:

Code snippet 1 (JSP ):
Out. println (request. getparameter ("username "));

This is a situation where you can directly start to use it regardless of whether the object is empty.

The function of section 1 is very simple, that is, to output the value of the form field "username" entered by the user.

It seems that no syntax error is found in the preceding statement, and in most cases there are no problems. However, if a user does not provide the value of the form field "username" when entering data, or directly enters the form by bypassing the form in some way, request. the value of getparameter ("username") is null (not an empty string, it is a null object .), The println method of the out object cannot directly operate on the null object. Therefore, the JSP page where code segment 1 is located will throw a "Java. Lang. nullpointerexception" exception.

Even if the object may be empty, some methods of Java. Lang. object or object itself, such as tostring () and equals (Object OBJ), are called.

Code snippet 2 (JSP ):
String username = request. getparameter ("username ");
If (username. Equals ("root "))
{....}


The function of section 2 is to check the user name provided by the user. If the user name is "root", special operations are performed.

In code snippet 2, if a user does not provide a value for the form field "username", the string object username is null and cannot be directly compared with another object, similarly, the JSP page where code snippet 2 is located will throw (Java. lang. nullpointerexception) NULL pointer error.

Analysis of Java NULL pointer exceptions (2)

The NULL pointer exception java. Lang. nullpointerexception is a common exception. We will discuss the analysis method of this exception in the article "Java NULL pointer exception analysis (1.
Code snippet 3 (JSP ):
String username = session. getattribute ("session. username"). tostring ();

The function of section 3 is to extract the value of session. username in the session and assign the value to the string object username.

In general, if a user has already performed a session, no problem will occur. However, if the application server restarts at this time, and the user has not logged on again, (The user may close the browser but still open the original page .) In this case, the session value becomes invalid, and the value of session. username in the session is empty. If you directly perform the tostring () operation on a null object, the system throws a null pointer exception (Java. Lang. nullpointerexception.
Solution:
To ensure that the objects to be operated or referenced are not empty, if we want to operate or reference an object, we first check whether the object has been instantiated and is not empty; in addition, add a processing method for the case where the object is null in the system. For example, the string object is used to save the results submitted by the user. If operations involving objects are performed, the system first checks whether the object is empty and then checks whether the object is empty, you can choose one of the following processing methods:

Processing Method (1): When the object is checked as null, set the object value to a null string or a default value;
Processing Method (2): When the object is detected to be null, no operation is performed, and the operation is directly redirected to other processes.
Processing Method (3): When the object is checked as null, the system prompts that the user operation has an error.

Rewrite code snippet 2 in the above way To get:
Method 1:
String username = request. getparameter ("username ");
// When the variable value is null, it is converted to a default null string.
If (username = NULL)
Username = "";
If (username. Equals ("root "))
{..........}

Method 2:
String username = request. getparameter ("username ");
// If the variable value is null, it is converted to a default null string without any operation.
If (usrename! = NULL)
{
If (username. Equals ("root "))
{..........}
}

Method 3:
String username = request. getparameter ("username ");
// If the variable value is null, it is converted to a default null string without any operation.
If (username = NULL)
{
// Prompt that user input is blank
}

In practice, the above three methods also apply to the handling of other exceptions:
1. Check whether an exception occurs. Set the object value to an empty string or a default value;
2. if an exception is detected and no operation is executed, the system jumps to another process.
3. Check whether an exception occurs and prompt the user for an operation error.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.