Several cases of java.lang.NullPointerException error

Source: Internet
Author: User

Java.lang.NullPointerException Several cases of error:

1. String variables not initialized;

2. Objects of the interface type are not initialized with a specific class, such as:

List stulist; In this case, the null pointer exception is reported;

List stulist = new ArrayList (); After initialization, no error is made.

3. When the value of an object is empty, and you do not determine the empty case, the null pointer exception is also reported;

So put a judgment in front of the code, like this:

if (ObjId! = null);

A string type object can be judged as follows:

if (Objectstr!) ==null &&! "". Equals (OBJECTSTR))

Of course, you can also make a more judgment, whether it is not an empty string:

if (Objectstr!) ==null &&! ". Equals (Objectstr.trim ()))

Example: When I edit a page, it pops up a custom error.jsp page code block (report system error)

Check the log: (the 54th line error--the picture has been badly cut)

Since Doc.getid did not make a judgment, when the ID does not exist The Times null pointer exception ... (For Case 3)

Several cases of java.lang.NullPointerException error

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.