2

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;


}
Vivek Giri
  • 465
  • 1
  • 6
  • 21

4 Answers4

5

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).

Dave McLure
  • 839
  • 1
  • 8
  • 10
3

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.

3
@GetMapping("/findAll")
@ResponseBody   
public Iterable<Customer> findAllCustomers() {

 return  repository.findAll();
}                               

When I am trying to do like this then I got the same exception

After that, I tried with iterating and added to List then I return that list so my problem solved.

try this one!

@GetMapping("/findAll")
@ResponseBody   
public List<Customer> findAllCustomers() {
    List<Customer> customers = new ArrayList<>();
    Iterable<Customer> customersAll = repository.findAll();
    for (Customer customer : customersAll) {
        customers.add(customer);
    }
 return customers;
}
G.Raju
  • 41
  • 2
1

I spend a whole day to fix an error like this. In my case I was having that Nullpointer because I had a Serializer that had a dependency that didn't instantiate, and the context of the original exception was gone! :'( so its message wasn't expressive enough. I had to debug many times until I find my Bean which wasn't properly instantiated. It has something to do with Spring MockMvc (I had to do a workaround, I mean, stop using this mock. Still looking forward to make it work ) In my case last external library method before finding my class was: com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(Object bean, JsonGenerator jgen, SerializerProvider prov)

It was here, last external class, from where I got NullPointerException. So my recommendation is track down that NullPointerException until you find the exact line. That will probably tell you what's happening.

Hope it helps.