Java tutorial
//package com.java2s; //License from project: Apache License import java.lang.reflect.Field; public class Main { public static void copyBeanWithOutNull(Object from, Object to) { Class<?> beanClass = from.getClass(); Field[] fields = beanClass.getFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; field.setAccessible(true); try { Object value = field.get(from); if (value != null) { field.set(to, value); } } catch (Exception e) { } } } }