简体   繁体   中英

JPA not mached query throwing null pointer exception using hibernat in spring boot

I using get hibernate to get data. When i called data using specific parameter which return not query result, it shows bellow error

java.lang.NullPointerException
    at com.rms.service.impl.ProductsServiceImpl.getAppProductItems(ProductsServiceImpl.java:143)
    at com.rms.controller.RmsMobileAppController.getAppProductItems(RmsMobileAppController.java:59)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62).......

Here is my entity

@Entity
@Table(name = "RmsOptions")
public class RmsOptionsEntity implements Serializable { 
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long id;    
    private String hardCode;
    private String softCode;
    private String codeDes;
    private String status;
    // getter setter
}

Here is my repository

public interface RmsOptionsRepo extends JpaRepository<RmsOptionsEntity, Long>{
    
      @Query(" FROM RmsOptionsEntity WHERE hardCode = ?1 AND softCode =?2 AND status = 1")
      RmsOptionsEntity getcodeDes(String hardCode,String softcord);
    
    }

I called from ProductsServiceImpl class which is my service class. if parameter values matched it is working fine. When parameter values found no match it shows the error... Here is my service

    @Service
public class ProductsServiceImpl implements ProductsService {   
  @Autowired
private ProductCategoriesRepo productCategoriesRepo;

    @Autowired
    private RmsOptionsRepo rmsOptionsRepo;
    
    @Override
    public List<ProductAppItemsModel> getAppProductItems(String restaurantId, String restaurantBranchId) {

        List<ProductAppItemsModel> list = new ArrayList<>();
        List<ProductCategoriesInfo> productCatgoriesList = productCategoriesRepo.getProductCatgoriesList(restaurantId, restaurantBranchId);
        try {
        
        for (ProductCategoriesInfo categoriesInfo:productCatgoriesList){            
            List<ProductsInfo> products = productsRepo.getProducts(restaurantId, restaurantBranchId, categoriesInfo.getProductCategoryId());

         } 
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

}

How to handle null pointer exception. I just used now try...catch. Please suggest me..

if you use jpaRepo its a better way to create a method

public interface RmsOptionsRepo extends JpaRepository<RmsOptionsEntity, Long>{

      @Query("SELECT * FROM RmsOptionsEntity WHERE hardCode = ?1 AND softCode =?2 AND status = 1")
      RmsOptionsEntity findCodeDes(String hardCode,String softcord);
    
    }

Shouldn't there be a SELECT in the start of the query?

public interface RmsOptionsRepo extends JpaRepository<RmsOptionsEntity, Long>{
    
      @Query("SELECT * FROM RmsOptionsEntity WHERE hardCode = ?1 AND softCode =?2 AND status = 1")
  RmsOptionsEntity getcodeDes(String hardCode,String softcord);    
}

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