With the basic Java data type, the value of the variable is either already the default value, and if it is not properly assigned, the program cannot compile, so using the basic Java data type (Double,float,boolean,char,int,long) generally does not cause a null pointer exception. This shows that the null pointer exception is mainly related to the operation of the object.
Here are a few scenarios where null pointer exceptions can occur and the corresponding solution:
Start using directly, regardless of whether the object is empty or not.
(JSP) Code Snippet 1:
Out.println (Request.getparameter ("username"));
Describe:
The function of code Snippet 1 is simple, which is to output the value of the form "username" entered by the user.
Description
It seems that the above statement could not find any grammatical errors, and in most cases it would not be a problem. However, if a user does not provide a value for the form field "username" when entering data, or if a way bypasses the form's direct input, the value of Request.getparameter ("username") is empty (not an empty string, which is null.) ), the println method of an Out object is not able to operate directly on an empty object, so the JSP page where code Snippet 1 is located will throw an "java.lang.NullPointerException" exception.
Even if the object may be empty, some methods such as ToString (), Equals (object obj), and so on, are called Java.lang.Object or object objects themselves.
(JSP) Code Snippet 2:
String userName = Request.getparameter ("UserName");
If (username.equals ("root"))
{....}
Describe:
The function of code Snippet 2 is to detect user-supplied user names and perform some special actions if the user is named "root".
Description
In code Snippet 2, if a user does not provide a value for the form field "username", the string object username to a null value, cannot compare a null object directly to another object, and the JSP page where Snippet 2 is located is thrown ( java.lang.NullPointerException) NULL pointer error.
(JSP) Code Snippet 3:
String userName = Session.getattribute ("Session.username"). ToString ();
Describe:
The function of code snippet 3 is to take the value of Session.username in the session and assign the value to the string object UserName.
Description
In general, there is no problem if the user has already made a session, but if the application server restarts at this point, and the user has not logged back in, the user may close the browser, but the original page is still open. At this point, the value of the session is invalidated, and the value of the Session.username in the session is null. The direct execution of the ToString () operation on a null object causes the system to throw a (java.lang.NullPointerException) null pointer exception.
Solution:
To ensure that the object being manipulated or referenced is non-null, if we are going to manipulate or reference an object, we first check to see if the object is instantiated and not empty, and to include processing of the case when the object is empty in the system.
For example, use a String object to save user-submitted results, and if the object is involved in the operation, first detect if it is empty, after checking that the object is empty, you can choose to do any of the following:
Processing mode 1) When the object is empty, set the object value to an empty string or a default value;
Processing mode 2) When an object is detected as empty, it does not perform an operation at all and jumps directly into other processing.
Processing mode 3) when the object is empty, the user is prompted for an error.
Rewrite code snippet 2 as above to get:
Mode 1:
String userName = Request.getparameter ("UserName");
When the value of this variable is null, it is converted to the default empty string
If (UserName = = null)
UserName = "";
If (username.equals ("root"))
{..........}
Mode 2:
String userName = Request.getparameter ("UserName");
When the value of the variable is null, it is converted to the default empty string and no action is taken.
If (Usrename! = null)
{
If (username.equals ("root"))
{..........}
}
Mode 3:
String userName = Request.getparameter ("UserName");
When the value of the variable is null, it is converted to the default empty string and no action is taken.
If (Usrename = = null)
{
Prompt for user input information is empty
}
In practice, the above provided to three processing methods also apply to other exception handling:
Exception handling 1) Check for exception occurrence, set object value to an empty string or a default value;
Exception handling mode 2) detects an exception, does not perform an operation at all, and jumps directly to other processes.
Exception handling mode 3) Check for an exception, prompting the user for an error.
Null pointer error java.lang.NullPointerException