简体   繁体   中英

NullPointerError on saveOrUpdate Hibernate

I am trying to update an object in this function:

public void priceLot(long lotId) throws Exception {
    Lot lot = this.getLotById(lotId);
    List list = bookService.getBooksByLotNumber(lotId);
    Book book = new Book();
    ProductAvailability pa = null;
    float retail = 0;
    for(int i = 0; i < list.size(); i++) {
        book = (Book) list.get(i);
        retail += book.getPrice();
    }
    if(retail == 0) {
        retail = 1;
    }
    float ratio = lot.getAmount() / retail;
    for(int i = 0; i < list.size(); i++) {
        book = (Book) list.get(i);
        pa = book.getProductAvailability();
        pa.setCost(pa.getRetail() * ratio);
        productAvailabilityService.saveOrUpdateProductAvailability(pa);
    }
    lot.setPriced(true);
    this.saveOrUpdate(lot);
}

It runs to the line

productAvailabilityService.saveOrUpdateProductAvailability(pa);

And here, it just dead-ends and returns NullPointerException . I don't know why it's doing this--nothing should be null. Here is the stack trace:

java.lang.NullPointerException
    com.samwellers.service.LotService.priceLot(LotService.java:69)
    com.samwellers.control.admin.lotadmin.PriceLotController.handleRequest(PriceLotController.java:29)
    org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:45)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:806)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:736)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:396)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:360)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)

There should not be anything null being passed to the saveOrUpdateProductAvailability -- and that function itself should have no problems. It works just fine in plenty of other places.

you should probable give some information. As far as I can see, both the pa variable as the productAvailibilityService could be null at this time where the exception is invoked.

Have you debugged and checked the pa is set correctly, and the service is also available?

I figured it out. I forgot to declare in my manifest that the service this function is within needed the ProductAvailabilityService. Therefore, there was no ProductAvailabilityService for it to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM