My exceptions-java. lang. NullPointerException, nullpointerexception

Source: Internet
Author: User

My exceptions-java. lang. NullPointerException, nullpointerexception

I believe everyone is very familiar with this exception. I believe that everyone has met this exception in their programming career. During the development of 3.1 of colleges and universities, such errors were thrown out again. This is a small bug, but when it comes to it, it will make you sick for half a day, because the program will be suspended and no longer provide normal services.

During basic development of external service interfaces, service exceptions are often encountered. When other systems call interfaces, data cannot be normally provided. Then I went to jboss to check the server. log, sent a message indicating the java. lang. NullPointerException error, and counted it with java. lang. NullPointerException, and found the same error at 481.


The reason is that when the development interface is implemented, the return value of the method does not determine whether the null value is returned directly.

Let's look at the implementation of two typical methods.

LstStudent = studentBean. queryStudentAll (); List fields = new ArrayList (); ObjectToMap otm = new ObjectToMap (); fields = Arrays. asList (Student. class. getFields (); List <Map <Serializable, Serializable> list = otm. convertToMap (fields, lstStudent); // use ObjectToMap. the return list is not judged before the convertToMap () method;

The above lstStudent is using ObjectToMap. when convertToMap () method is used, if it is not determined whether it is null, it is directly converted. Once it is null, the returned value list is null, leading to this error.

Public List <Map <Serializable, Serializable> queryColleageNamesByIds (List <String> colleageIds, String dataBaseName) {List <Map <Serializable, Serializable> listMapInstitution = new ArrayList <Map <Serializable, serializable> (); List <Institution> listInstitution = new ArrayList <Institution> (); listInstitution = institutionEao. queryByList (colleageIds, dataBaseName); if (listInstitution! = Null | listInstitution. size ()> 0 | listInstitution. get (0 )! = Null) {EntityToMap entityToMap = new EntityToMap (); listMapInstitution = entityToMap. entityToMap (listInstitution); return listMapInstitution;} else {return null; // at least the new List <Map <Serializable, Serializable> should be returned, so that java is not reported. lang. nullPointerException exception }}

The error of this method is obvious. If the query fails, a null value is directly returned. at least the new List <Map <Serializable, Serializable> should be returned, so that java is not reported. lang. nullPointerException exception.

Looking at the previous learning records, I found some summary of the null and "" methods, and then I want to share it with you. I hope it will not be troubled by java. lang. NullPointerException in the future.

 

If we want to write a name object like this


if (name =="") {     //do something}

Or write as follows:

if(name.equals("")) {     //do something}

The former is the easiest and least noticeable mistake for beginners, because their syntax itself is fine and the Java compiler does not report an error during compilation. However, this condition may cause a bug in the program during running and will never be true. That is to say, the statements in the if block will never be executed.

The latter, including many errors that are easy to make in Java, is it wrong? Why? You may be wondering.

Yes, they are correct in writing. However, with a null condition missing, imagine what would happen if name = null? The consequence is that your program will throw an NullPointerException and the system will be suspended to stop providing normal services.

Of course, if you have already made a null judgment on name, this is an exception.

 

Name should be added first for correct writing! = Null condition, for example:

 

If (name! = Null &&! Name. equals ("")){

// Do something

}

 

Or

 

If (! "". Equals (name )&&! Null. equals) {// write "" in front, so that no error occurs no matter whether the name is null or not.

// Do something

}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.