简体   繁体   中英

java.lang.NullPointerException while getting data from db using @Query in spring JPA. Though query returns data in MySql

I am trying to get data from DB using pincode and cityName but I am getting null pointer exception. I tried to run the query in MySql. It returns correct result. pincode and cityName are getting correct parameters.

@Component
public interface GeoMasterDao extends PagingAndSortingRepository<GeoMaster, Long>,JpaSpecificationExecutor<GeoMaster>{

    @Query("select p from GeoMaster p where p.pincode =:pincode and p.cityName =:cityName")
    GeoMaster findByPincodeAndCityName(@Param("pincode") String pincode, @Param("cityName") String cityName);
}
@Entity
@Table(name = "geo_master")
@JsonIgnoreProperties("isBlocked")
@Getter
@Setter
public class GeoMaster extends AbstractEntity {

    @Column
    private String countryId;

    @Column
    private String districtId;

    @Column
    private String stateId;

    @Column
    private String pincode;

    @Column
    private String cityId;

    @Column
    private String cityName;

}

I am calling it using GeoMaster gm = gmDao.findByPincodeAndCityName(pincode, cityName);and here I am getting NullPointerException. findByPincodeAndCityName is having correct values in parameters.

The null pointer exception is probably happening because gmDao is null, rather than some problem with the query. You should check to make sure that your DAO object is being autowired correctly. You should have something like this:

@Autowired
public GeoMasterDao gmDao;

in the class which is executing the query.

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