-1

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;
    }
}

My OrderProduct in database

fr3ddie
  • 376
  • 5
  • 17

1 Answers1

0

OrderProductRepository is not initialized. Add it to the constructor for it be autowired by Spring Boot:

public OrderController(ProductService productService, OrderService orderService, OrderProductService orderProductService, OrderProductRepository orderProductRepository) {
    ...
    this.orderProductRepository = orderProductRepository;
}

I've not seen your code but may you've jumbled OrderProductRepository with OrderProductService here:

@GetMapping("/getAllOrders")
public List<OrderProduct> listOrderActive() {
    return orderProductRepository.findByActiveFalse();
}

And add @Repository annotation above the repository.

fr3ddie
  • 376
  • 5
  • 17
  • Yes I have just missed that ! I don't understand so much why you must put it into constructor but ok ! After that for a list I must add to my user entity @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) and spring.jackson.serialization.fail-on-empty-beans=false to my application.properties to have all the listing result of my query ! – Aloïs Coussout Nov 09 '20 at 18:32
  • thank you for your answer ;) – Aloïs Coussout Nov 09 '20 at 18:33
  • Your Controller class is annotated using a spring boot annotation (`@RestController` in this case). Spring automatically inject a repository because repository inherits from `CrudRepository` and controller is marked by `@Rest controller`(this is only my observation but they seem to reflect reality). It's just Spring Boot magic ;) – fr3ddie Nov 09 '20 at 18:48
  • No problem. Every answer learn me a lot. ;) – fr3ddie Nov 09 '20 at 18:50
  • If you want I have posted another question about query ^^ – Aloïs Coussout Nov 09 '20 at 21:23
  • If you have some doubt you can do it. But I don't need. ;) – fr3ddie Nov 09 '20 at 22:40