Reputation: 425
I have an object of type User
(as defined below) which upon serialization to Json throws this particular error:
com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: com.providenceuniversal.gim.AuthenticationRequest["user"]->com.providenceuniversal.gim.User["lastSeenToLocal"])
User
definition (with relevant properties and methods):
public class User implements ServerMessage {
//.....
private final @JsonInclude(Include.NON_NULL) Instant lastSeen;
@JsonCreator
User(@JsonProperty("username") String username) {
if (!isValidUsername(username))
throw new IllegalArgumentException("Invalid constructor argument values");
this.username = username.trim();
this.firstName = null;
this.lastName = null;
this.email = null;
this.status = null;
this.joinDate = null;
this.lastSeen = null;
this.dateOfBirth = null;
}
@JsonCreator
User(
@JsonProperty("username") String username,
@JsonProperty("firstName") String firstName,
@JsonProperty("lastName") String lastName,
@JsonProperty("email") String email,
@JsonProperty("status") String status,
@JsonProperty("joinDate") Instant joinDate,
@JsonProperty("lastSeen") Instant lastSeen,
@JsonProperty("dateOfBirth") LocalDate dateOfBirth) {
if (username == null || username.trim().length() == 0)
throw new IllegalArgumentException("Invalid constructor argument values");
this.username = username.trim();
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.status = status;
this.joinDate = joinDate;
this.lastSeen = lastSeen;
this.dateOfBirth = dateOfBirth;
}
//.....
public Instant getLastSeen() {
return lastSeen;
}
public LocalDateTime getLastSeenToLocal() {
return getLastSeen().atZone(ZoneId.systemDefault()).toLocalDateTime();
}
From the exception it's obvious that the problem is being caused by the getLastSeenToLocal()
method (which tries to manipulate a null
object in lastSeen
), whose relevance to the serialization process I can't see. Does Jackson call all getters by default, whether or not the fields they return are listed as JsonProperty
s, or there is something I'm missing (clearly)?
Upvotes: 2
Views: 2659
Reputation: 2506
Jackson by default uses getters() of class. So you can
Upvotes: 2