1

I have a situation when a user does not enter data for certain field I pass null as value for that int field to signify that no value was entered by the user like

{user: 'John',
 age: null}

but when I read such document in my Spring Boot application, when it encounters above document, throws

Caused by: java.lang.IllegalArgumentException: Parameter age must not be null!

Is null allowed value in mongodb or Spring Boot is doing something wrong?

I tried to:

@Data
@Document
public class User {
    final private String user;
    @org.springframework.lang.Nullable    
    final private int age;
}

But it makes no difference. How to solve this problem other than not storing null ( because null is already populated in another node/mongoose application (which gladly stores and reads null values without any issue) using the same mongodb database?

3
  • What are u seeing if you change age as Integer instead of int ? Commented Apr 4, 2018 at 18:40
  • I replaced int with Integer and it fixed the problem. Not sure why it did not work with int. thank you Commented Apr 4, 2018 at 19:08
  • added explanation below, accept if that worked for you :) Commented Apr 4, 2018 at 19:10

1 Answer 1

7

Replace int with Integer since int is a primitive type which won't accept null values. Integer is a wrapper object that accepts null values.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.