Reputation: 485
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;
}
Upvotes: 4
Views: 64377
Reputation: 1
It can also happen if the data types of data you are trying to map don't match. In my case, this happened when I tried to retrieve a .jpg file. I mapped it to MultipartFile instead of byte[]. Changing it to byte[] resolved it.
public interface GetTournamentProjection {
String getTournamentName();
String getSponsors();
**MultipartFile** getPoster();
}
I have changed it to this:
public interface GetTournamentProjection {
String getTournamentName();
String getSponsors();
**byte[]** getPoster();
}
It worked!!. Try to debug or use print statements to see if it reaches the repository file. If yes then most probably it's a mapping issue. Resolve it by carefully looking for datatype mismatch between entity and Projection files
Upvotes: 0
Reputation: 77
I had faced same issue. Turned out I had changed a POJO field to Integer from int. But forgot to change its getter and setters from int to Integer.
Upvotes: 0
Reputation: 41
@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;
}
Upvotes: 3
Reputation: 3367
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.
Upvotes: 1
Reputation: 51
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.
Upvotes: 4
Reputation: 919
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).
Upvotes: 6