Gambit2007
Gambit2007

Reputation: 3958

Spring Boot - Source must not be null

I created a simple Spring Boot app with one endpoint that creates a new record in a users table.

However, when i hit this endpoint with Postman, i'm getting the following error:

java.lang.IllegalArgumentException: Source must not be null
    at org.springframework.util.Assert.notNull(Assert.java:198) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:693) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:639) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at com.demo.controller.UserController.createUser(UserController.java:45) ~[classes/:na]

What is the issue?

EDIT:

Apparently it does save the record in the DB, but returns an error still. Here's the code for UserController:

    @PostMapping
    public UserRest createUser(@RequestBody UserDetailsRequestModel userDetails) 
    {

        UserRest result = new UserRest();
        UserDto userDto = new UserDto();

        BeanUtils.copyProperties(userDetails, userDto);

        UserDto createdUser = userService.createUser(userDto);

        BeanUtils.copyProperties(createdUser, result);
        return result;
    }

Upvotes: 2

Views: 18403

Answers (1)

Vova Bilyachat
Vova Bilyachat

Reputation: 19474

Java spring has asserting that parameter can't be null. So as you mentioned you are calling method

BeanUtils.copyProperties(createdUser, result)    at com.demo.controller.UserController.createUser(UserController.java:45) ~[classes/:na]

You simply need to make sure that values are not null

If you look in source of copy method you will find check for not to be null.

Assert.notNull(source, "Source must not be null");
Assert.notNull(target, "Target must not be null");

Upvotes: 4

Related Questions