简体   繁体   中英

NullPointerException - check null on class

How to know the Employees.class is null or not using Java Relection?

Example Class: Employees.class

Problem:

  • employees.getName();
  • value getName is Mikel
  • employees is null
  • if employess != null is TRUE.
  • this class already 'new Employees()'

Employees.Class

public class Employees{

private Long id;<br>
private String kodStuff;<br>
private String Name;<br>

//Getter and Setter<br>
}

Example Process Method:

public void check(){
    Employees employees = new Empoyees;
    employees.setName("Mikel");
    employees.setKodStuff("KL370");
    //employees not save yet and process redirect check value of Name wuth return boolean
    Boolean empty = checkName(employees);

}

public boolean checkName(Employees employees){
    boolean notNull = false;
    if(employees !=null && StringUtils.isNotBlank(employees.getName())){ // this is line 11
        notNull = true;
    }
    return notNull;
}

Throws exception at line no '11':

java.lang.NullPointerException

How can i check null exception during process on line no.11?

尝试通过执行以下操作检查employees.getName()是否为空:
employees.getName() != null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM