Null Pointer (Java. Lang. nullpointerexception)

Source: Internet
Author: User

 

When any method is called for a null pointer in Java, a null pointer exception (java. lang. NullPointerException) is thrown ). Null Pointer exceptions are definitely the most difficult to find and debug in Java, and you will never get any useful debugging information. Summarized as follows through daily summary and online collection for your reference. The main causes of NULL pointer exceptions are as follows:
1. The string variable is not initialized;
2. The object of the interface type is not initialized with a specific class, for example:
List lt; will report an error
List lt = new ArrayList (); no error is reported
3. When the value of an object is null, you are not sure whether it is null. You can try to add a line of code before the following code:
if(rb!=null && rb!="")
Changed:
if(rb==null);
If (rb! = Null & rb! = "") Or if (""). equals (rb ))

Null Pointer solution:
      Focus on the row where the error is reported, and diagnose the specific error based on the two main causes of the NULL pointer exception. To avoid the occurrence of null pointers, it is recommended that the "null" or null value be placed before the set value during judgment and 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. If you add a variable without assigning a value to it, the Java Virtual Machine cannot
Therefore, using the basic Java data type will not cause NULL pointer exceptions. In actual development, most NULL pointer exceptions are mainly related to object operations.

The following lists the possible situations and solutions for NULL pointer exceptions:
Code Segment 1:
Out. println (request. getParameter ("username "));
Analysis: the function of code segment 1 is very simple, that is, to output the user input "username" value.
      Note: It seems that no syntax error is found in the preceding statement, and in most cases, no problem occurs. However, if a user does not provide the value of the form field "username" when entering data, or directly enters a form by bypassing the form in some way, this request. the value of getParameter ("username") is null (note that it is not a null string and 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. In addition, even if the Object may be empty, some methods of Java. lang. Object or Object itself, such as toString (), are called (),
Equal (Object obj.

Code Segment 2:
String userName = request. getParameter ("username ");
If (userName. equals ("root "))
{....}
Analysis: the function of code segment 2 is to check the user name provided by the user. If the user name is "root", perform some special operations.                                                                     

   Note: In code segment 2, if a user does not provide a value for the form field "username", the string object username is null, A null object cannot be directly compared with another object. Similarly, the JSP page where code snippet 2 is located will throw a null pointer error.

TIPS: If you want to compare the return value of a method with a constant and put the constant before it, you can avoid calling the equals method of a null object. For example:

 If ("root". equals (userName ))
{....}
 
 Even if the userName object returns a null object, there will be no null pointer exception here and it can run as usual.

Code Segment 3:
String userName = session. getAttribute ("session. username"). toString ();
       Analysis: the function of code segment 3 is to extract the value of session. username in the session and assign the value to the string object userName.
      Note: In general, if a user has already performed a session, no problem occurs. However, if the application server is restarted 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.

Code segment 4:
Public static void main (String args []) {

      Person p = null;

P. setName ("James ");

   System. out. println (p. getName ());

}

Analysis: declare a Person object and print the Name of the object.

Note: At this time, your p has a null pointer exception, because you just declared that this Person type object has not created an object, so there is no address reference in its heap, do not create an object when using the object drop method.

 

 

Http://blog.sina.com.cn/s/blog_553d5e730100eg8g.html

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.