0

BeanUtils.copyProperties() throw NullPointerException on Tomcat, but on localhost it works fine.

I've checked everything and dto isn't null, only the properties of user is null which is fine.

public void saveUser(UserDTO dto) {
    User user = new User();
    BeanUtils.copyProperties(dto, user);

    session.beginTransaction();
    session.save(user);
    session.getTransaction().commit();
}

my User.java

public class User {

    @Id @GeneratedValue
    private long id;

    private String userId;
    private String firstName;
    private String lastName;
    private String email;
    @Enumerated(EnumType.STRING)
    private Role role;
    private String encryptedPassword;
    private String salt;
    private String token;

... getters and setters method ...
}
Pakanon Pantisawat
  • 105
  • 1
  • 1
  • 7

1 Answers1

0

The error is when BeanUtils try to copy Role role from your User class

private Role role;

It will try to get properties of Role (via reflection) but because your role is null, it throw NPE

Use this method to ignore field role while copying:

public static void copyProperties(Object source,
                              Object target,
                              String... ignoreProperties)
Mạnh Quyết Nguyễn
  • 15,590
  • 1
  • 17
  • 41