My abnormal-java.lang.nullpointerexception.

Source: Internet
Author: User

This anomaly believe that everyone must be very familiar with, I believe in each person's program development career have met. In the development of 3.1 in this university, this kind of mistake has been thrown out. This is a small bug, but when it comes to it, it can make you nauseous for a while because the program hangs and no longer provides the normal service.

In the basic development of the external service interface, often encountered service anomalies, other systems in the interface, sometimes do not provide the normal data. Then I went to JBoss to look at Server.log, sent a report java.lang.NullPointerException the wrong, and then to Java.lang.NullPointerException count, the same error found at 481.


The reason for this is that the return value of the method is not judged when the implementation of the interface is developed. NULL value is returned directly

See two more typical ways to achieve

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);// No return list is determined before using the Objecttomap.converttomap () method;

The above lststudent when using the Objecttomap.converttomap () method, does not determine whether it is null, the direct conversion, once a null value, the return value list is null, resulting in the occurrence of the 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;//should return at least new list<map<serializable, Serializable>> This does not report java.lang.NullPointerException exception}}

the error of the method is obvious, and if the query is not reached, it simply returns a null. Should return at least new list<map<serializable, serializable>> , so as not to report java.lang.NullPointerException exception.

look at the previous study record and find some null and "" Method summary, and then take out to share, hope in the future no longer be java.lang.NullPointerException the troubled

If we're going to have a Name object that we tend to get used to writing like this


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

or write this:

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

The former, beginners are most likely to commit, but also the most difficult to find errors, because their grammar itself is not a problem,theJava compiler compiles without error. However, this condition may cause a bug in the program at run time and will never be true, that is, the statement in the If block will never be executed.

the latter, including many Java Skilled is also very easy to make mistakes, why is wrong? Maybe you'll be wondering.

Yes, they're written in their own right, but one less. NULL to judge the condition, just imagine, if Name=null case, what will happen? The consequence is that your program will throw NullPointerException exceptions, the system will be suspended and no longer provide normal service.

of course, if you have previously name is a null-judged case exception.

the correct wording should be preceded by the condition of the name! = NULL, as an example:

if (name!= null &&!) name.equals ("")) {

Do something

}

Or

if (! "". Equals (name) &&!null.equals) {// writes "" to the front so that no error occurs regardless of whether or not the name is null.

Do something

}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

My abnormal-java.lang.nullpointerexception.

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.