Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON

org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: (was java.lang.NullPointerException) (through reference chain: in.xyz.sync.dto.ClassRoom["id"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: in.xyz.sync.dto.ClassRoom["id"])

import java.util.List;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.map.annotate.JsonSerialize;

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class ClassRoom extends CommonResponse {

    private long classId;
    private String clientClassRoomId;
    private long instituteId;
    private long teacherId;
    private String className;
    private long handleId;
    private int archived;
    private int deleted;
    private String creationTime;
    private String modifiedTime;
    private int type;
    private String classCode;
    private List<Student> student;
    private String handle;
    private String firstName;


}
like image 269
Vivek Giri Avatar asked Sep 01 '25 10:09

Vivek Giri


2 Answers

You may have better results switching from primitives (int & long) to Java classes (Int & Long) in your ClassRoom class. See JsonMappingException (was java.lang.NullPointerException)

You might also have luck pinning down which field is null by assigning @NotNull (javax.validation.constraints.NotNull) to all of your fields. If you are encountering a null value, the @NotNull annotation will cause a runtime exception. Then, comment out the @NotNull annotations one by one until the exception goes away and you will find the culprit (or at least the first one of them if there are more than one).

like image 132
Dave McLure Avatar answered Sep 04 '25 03:09

Dave McLure


In my case, i just had to replace the int by Integer:

e.g.:

private int idtask; => private Integer idtask;

Did the same for getter and setter.

public Integer getIdlayer() {
        return idlayer;
    }

public void setIdlayer(Integer idlayer) {
    this.idlayer = idlayer;
}

This solved my issue. Basically i was having this issue because there was a null value in this column. Using: Spring Boot + PostgreSQL

Note: Dave McLure' answer is the solution too.

like image 23
Miguel Ramos Avatar answered Sep 04 '25 04:09

Miguel Ramos