判断对象的属性值是否为null


判断对象的属性值是否为null

核心处理:

    private Object getFieldValueByName(String fieldName, Object o) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = o.getClass().getMethod(getter, new Class[] {});
            Object value = method.invoke(o, new Object[] {});
            return value;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        }
    }

 

运用示例:

        DoctorVo doctorVo = new DoctorVo();
        // doctorVo.setId(3942);
        Object id = this.getFieldValueByName("id", doctorVo);
        if (null != id) {
            bdId = doctorVo.getId();
            logger.info("传递了医生ID,doctorId = " + bdId);
        }

 

补充说明(总结):

  本问题的产生,主要是  “引用数据类型”  拆箱为 “基本数据类型”(即相应的对象类型,如Integer -- -int,Double --- double …)产生的问题。

  避免的方法:尽量不做装箱、拆箱的操作,定义、传递、转换和使用的过程中,尽量保持数据类型一致。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM