A null pointer exception (JAVA.LANG.NULLPOINTEREXCEPTION) is thrown when any method is called in Java for a pointer with a value of NULL. Null pointer exceptions are definitely one of the hardest exceptions to find and debug in Java, and you'll never get any useful debugging information. Through the personal daily summary and online collection and collation, summarized as follows, for everyone's reference. The main reasons for the exception of NULL pointers are as follows: Therefore, there are several reasons for the general reporting of NULL pointer exceptions in Java:
1 string variable not initialized;
Objects of the 2 interface type are not initialized with a specific class, such as:
List lt; Will error
List lt = new ArrayList (); You won't get an error.
3 when the value of an object is empty, you are not judged to be empty. You can try adding a line of code to the following code:
if (rb!=null && rb!= "")
Change to:
if (rb==null);
if (rb!==null&&rb!= ") or if ((" "). Equals (RB))
Workaround for NULL pointers:
Focus on the row where the error occurred, and diagnose the specific error with the two main causes of the null pointer exception. At the same time, in order to avoid the occurrence of null pointers, it is best to put "null" or null value before the set value when doing judgment processing.
Brief analysis of common NULL pointer exceptions:
(1) NULL pointer error
8 basic data types in Java, the value of a variable can have its default value, the addition does not have its normal assignment, the Java Virtual machine is not
Compiled correctly, so using basic Java data types will not normally cause null pointer exceptions. In actual development, most of the null pointer exceptions are related to the operation of the object.
Here are a few scenarios where null pointer exceptions can occur and the corresponding solution:
Code Snippet 1:
Out.println (Request.getparameter ("username"));
Analysis: The function of code Snippet 1 is very simple, that is, the output user input "username" value.
Description: It appears that the above statement could not find any syntax errors and that in most cases there were no problems. However, if a user does not provide a value for the form field "username" when entering the data, or if a way bypasses the form's direct input, the value of this request.getparameter ("username") is empty (note that it is not an empty string, and Null is an empty object). ), 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. And even if the object may be empty, some methods such as ToString (), equal (object obj), and so on, are called Java.lang.Object or object objects themselves.
Code Snippet 2:
String userName = Request.getparameter ("UserName");
If (username.equals ("root"))
{....}
Analysis: The function of code Snippet 2 is to detect user-provided user name, if the user name is "root" user, do some special action.
Note: 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 will throw a null pointer error.
A little trick: if you want to compare the return value of a method to a constant, put the constant in front, you can avoid calling the Equals method of the null object. Such as:
If ("root". Equals (UserName))
{....}
Even if the Username object returns a null object, there is no null pointer exception, which can be run as usual.
Code Snippet 3:
String userName = Session.getattribute ("Session.username"). ToString ();
Analysis: The function of code snippet 3 is to remove the value of Session.username in the session and assign the value to the String object username.
Note: 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 still open the original page. 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 null pointer exception.
Code Snippet 4:
public static void Main (String args[]) {
Person P=null;
P.setname ("Zhang San");
System.out.println (P.getname ());
}
Parse: Declares a person object and prints out the name of the name in the object.
Note: This time your p will appear null pointer exception, because you just declared that the person type object and did not create the object, so it does not have the address reference in the heap, should not be used when you want to use the Object drop method must be created object.
Java NULL pointer exception (JAVA.LANG.NULLPOINTEREXCEPTION)