0

Am writing spring boot rest api to fetch records from elastic search by id. While am getting response from elastic search few keys having null object. So, how I can do the null check while setting value for Product (POJO class).

Am handling this exception with the below line of code, but still the error is persisting product.setExpiration_date(sourceAsMap.get("expiration_date").toString() != null ? sourceAsMap.get("expiration_date").toString() : "");.

Am not sure whether my approach is correct or not.

Please find the full code below.

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());

    }

Please find my error below :

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]
A J
  • 1,439
  • 4
  • 25
  • 42
Karthikeyan
  • 1,927
  • 6
  • 44
  • 109
  • 2
    if "sourceAsMap.get("expiration_date")" is the key which has a null value then calling toString is like "null.toString()" which will always give you a NullPointerException – Lalit Mehra May 24 '18 at 03:52
  • 1
    sourceAsMap.get("expiration_date").toString() .. have you checked that sourceAsMap.get("expiration_date") is null or not? – Amit May 24 '18 at 05:46
  • 1
    With Apache Commons Collections library, you may use the MapUtils.isEmpty() methods which respectively check if a map is empty or null (i.e. they are "null-safe"). – Amit May 24 '18 at 05:48

1 Answers1

1

When you check for null element don't need call a method on them.

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

becomes

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

Otherwise you are calling to string on a null

rick
  • 1,869
  • 13
  • 22