Hello I got a strange issue with a query on Java-springboot.
java.lang.NullPointerException: Cannot invoke "fr.MelodyApp.repository.OrderProductRepository.findByActiveFalse()" because "this.orderProductRepository" is null\
I want to get a list of all OrderProduct Who are still active ! So I have done a query who desperatly return null value. And I don't know why .. I tried with query on SQL it works fine ! I'm asking maybe is due to conversion problem between java and MySQL. I'm looking for some advice ;)
public interface OrderProductRepository extends CrudRepository<OrderProduct, OrderProductPK> {
@Query("SELECT o FROM OrderProduct o WHERE o.active = 0")
public List<OrderProduct> findByActiveFalse();
}
@RestController
@RequestMapping("/api/auth/orders")
@CrossOrigin()
public class OrderController {
ProductService productService;
OrderService orderService;
OrderProductService orderProductService;
UserService userService;
OrderProductRepository orderProductRepository;
public OrderController(ProductService productService, OrderService orderService, OrderProductService orderProductService) {
this.productService = productService;
this.orderService = orderService;
this.orderProductService = orderProductService;
}
@GetMapping("/getAllOrders")
public List<OrderProduct> listOrderActive() {
return orderProductRepository.findByActiveFalse();
}
@Entity
@Table(name="OrderProduct")
public class OrderProduct {
@EmbeddedId
@JsonIgnore
private OrderProductPK pk;
@Column(name="QUANTITY", nullable = false) private Integer quantity;
@Column(name="USER", nullable = false) private Integer user;
@Column(name="ORDERING_DATE", nullable = true) private Date dateOrdering;
@Column(name="DELIVER_DATE", nullable = true) private Date dateDeliver;
private boolean active;
public OrderProduct() {
super();
}
public OrderProduct(Order order, Product product,Integer user, Integer quantity) {
pk = new OrderProductPK();
pk.setOrder(order);
pk.setProduct(product);
this.user = user;
this.quantity = quantity;
}
@Transient
public Product getProduct() {
return this.pk.getProduct();
}
@Transient
public Double getTotalPrice() {
return getProduct().getPrice() * getQuantity();
}
public OrderProductPK getPk() {
return pk;
}
public void setPk(OrderProductPK pk) {
this.pk = pk;
}
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
public Integer getUser() {
return user;
}
public void setUser(Integer user) {
this.user = user;
}
public Date getDateOrdering() {
return dateOrdering;
}
public void setDateOrdering(Date dateOrdering) {
this.dateOrdering = dateOrdering;
}
public Date getDateDeliver() {
return dateDeliver;
}
public void setDateDeliver(Date dateDeliver) {
this.dateDeliver = dateDeliver;
}
public Boolean getActive() {
return active;
}
public void setActive(Boolean active) {
this.active = active;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((pk == null) ? 0 : pk.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
OrderProduct other = (OrderProduct) obj;
if (pk == null) {
if (other.pk != null) {
return false;
}
} else if (!pk.equals(other.pk)) {
return false;
}
return true;
}
}