枚举零指针exeption
我在请求参数中使用了枚举来接受品牌价值,但这不是强制性领域。因此,我使用quired = false
,但是当我传递品牌null
或在参数中空的时,它给出了null-pointer异常。请告诉我如何解决此问题。
public enum Brand {
Avis("A"),
Budget("B"),
Payless("P");
private String value;
public String getvalue() {
return value;
}
Brand(String value) {
this.value = value;
}
}
@GetMapping("/locations") // getlocation
public ResponseEntity<?> searchlocationbyproximity(
@RequestParam(value = "brand", required = false ) Brand brand,
@RequestParam(value = "country_code") String country_code,
@RequestParam(value = "postal_code", required = false) String postal_code,
@RequestParam(value = "radius") String radius,
@RequestParam(value = "state_code", required = false) String state_code,
@RequestParam(value = "street_name", required = false) String street_name,
@RequestParam(value = "city", required = false) String city,
@RequestParam(value = "transaction_id", required = false) String transaction_id
) throws Exception {
I have used enum in the request parameter to accept brand values, but it's not a mandatory field. So I used required=false
, but when I am passing brand null
or empty in a param it's giving a null-pointer exception. Please show me how to fix this.
public enum Brand {
Avis("A"),
Budget("B"),
Payless("P");
private String value;
public String getvalue() {
return value;
}
Brand(String value) {
this.value = value;
}
}
@GetMapping("/locations") // getlocation
public ResponseEntity<?> searchlocationbyproximity(
@RequestParam(value = "brand", required = false ) Brand brand,
@RequestParam(value = "country_code") String country_code,
@RequestParam(value = "postal_code", required = false) String postal_code,
@RequestParam(value = "radius") String radius,
@RequestParam(value = "state_code", required = false) String state_code,
@RequestParam(value = "street_name", required = false) String street_name,
@RequestParam(value = "city", required = false) String city,
@RequestParam(value = "transaction_id", required = false) String transaction_id
) throws Exception {
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论