枚举零指针exeption

发布于 2025-01-30 06:20:05 字数 1095 浏览 1 评论 0原文

我在请求参数中使用了枚举来接受品牌价值,但这不是强制性领域。因此,我使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文