I am getting this null pointer exception on the id of the newly created object that I'm putting into the DB, even though it should be auto generated.
@Entity
@Table(name = "REQUEST")
@EntityListeners(DatabaseListener.class)
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "requestId", scope = Request.class)
public class Request implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "REQUEST_ID", nullable = false)
private Long requestId;
@Column(name = "REQUEST_NAME")
private String name;
@Column(name = "CATEGORY")
private String category;
@Column(name = "REQUEST_MESSAGE")
private String requestMessage;
@Column(name = "CREATED_ON")
private Date createdOn;
@ManyToOne
private User createdBy;
@ManyToOne
private User assignedTo;
@ManyToOne
private Status status;
@OneToMany(mappedBy = "request", fetch = FetchType.EAGER)
private List<Response> replies;
The endpoint that I am calling...
@RequestMapping(value = "/requests/addRequest", method = RequestMethod.POST)
public ResponseEntity<Request> addRequest(@RequestBody final Request request) {
if(request.getStatus() == null){
request.setStatus(request.getAssignedTo() == null ? statusRepo.findByType(StatusType.UNASSIGNED) : statusRepo.findByType(StatusType.ASSIGNED));
}
final Request saved = requestRepo.save(request);
return new ResponseEntity<>(saved, HttpStatus.CREATED);
}
I can add more of the other classes if needed as well. But I can not figure out why requestId would be null. I'm calling that enpoint from an app and passing it a Request
object, so obviously there it is null but it shouldn't be null when it's going into the DB. Right?
org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: (was java.lang.NullPointerException) (through reference chain: Request["requestId"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: Request["requestId"])
...
Caused by: com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain:Request["requestId"])
...
Caused by: java.lang.NullPointerException: null
at com.fasterxml.jackson.annotation.ObjectIdGenerator$IdKey.<init>(ObjectIdGenerator.java:125) ~[jackson-annotations-2.4.0.jar:2.4.0]
Edit: To be clear, I know what a null pointer exception is, I understand that the requestId is null and that is causing a problem, so I don't think this is a duplicate question. What I don't understand is why it is null. It should be auto generated, but as Jason pointed out, I may have to determine the id application side first.