【问题标题】:Handling null pointer exception is not working [duplicate]处理空指针异常不起作用[重复]
【发布时间】:2018-11-03 03:40:48
【问题描述】:

我正在编写 spring boot rest api 来通过 id 从弹性搜索中获取记录。虽然我从弹性搜索中得到响应,但很少有 keys 拥有 null 对象。 那么,如何在为Product(POJO 类)设置值时进行空检查。

我正在使用以下代码行处理此异常,但错误仍然存​​在product.setExpiration_date(sourceAsMap.get("expiration_date").toString() != null ? sourceAsMap.get("expiration_date").toString() : "");

我不确定我的方法是否正确。

请在下面找到完整的代码。

SearchResponse searchResponse = null;
    try {
         searchResponse = restHighLevelClient.search(searchRequest);
    } catch (IOException e) {
        e.getLocalizedMessage();
    }
    // read the response
    String productName = null;
    Product product = null;
    SearchHit[] searchHits = searchResponse.getHits().getHits();
    for (SearchHit hit : searchHits) {
        // get each hit as a Map
        Map<String, Object> sourceAsMap = hit.getSourceAsMap();
        product=new Product();
        product.setName(sourceAsMap.get("name").toString());
        product.setCatalog_id(sourceAsMap.get("catalog_id").toString());
        product.setCode(sourceAsMap.get("code").toString());
    product.setExpiration_date(sourceAsMap.get("expiration_date").toString() != null ? 
                sourceAsMap.get("expiration_date").toString() : ""); // Error throwing in this line.
        product.setIs_visible(sourceAsMap.get("is_visible").toString());

    }

请在下面找到我的错误:

2018-05-23 22:59:13.216 ERROR 9948 --- [nio-8594-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null
at com.sun.searchengine.dao.ProductDao.getProductById(ProductDao.java:105) ~[classes/:na]
at com.sun.searchengine.controller.ProductController.getProductById(ProductController.java:24) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]

【问题讨论】:

  • 如果 "sourceAsMap.get("expiration_date")" 是具有空值的键,那么调用 toString 就像 "null.toString()" 总是会给你一个 NullPointerException
  • sourceAsMap.get("expiration_date").toString() .. 你检查过 sourceAsMap.get("expiration_date") 是否为空吗?
  • 使用 Apache Commons Collections 库,您可以使用 MapUtils.isEmpty() 方法分别检查地图是否为空或为空(即它们是“空安全的”)。

标签: java rest spring-boot elasticsearch


【解决方案1】:

当您检查空元素时,不需要对它们调用方法。

sourceAsMap.get("expiration_date").toString() != null ? 
                sourceAsMap.get("expiration_date").toString() : ""

变成

sourceAsMap.get("expiration_date") != null ? 
                sourceAsMap.get("expiration_date").toString() : ""

否则你会调用空字符串

【讨论】:

    猜你喜欢
    • 2021-11-08
    • 1970-01-01
    • 2016-09-11
    • 2014-02-04
    • 1970-01-01
    相关资源
    最近更新 更多