java.lang.NullPointerException

Spring JIRA | chris snow | 7 years ago
tip
Do you know that we can give you better hits? Get more relevant results from Samebug’s stack trace search.
  1. 0

    I am trying to call a rest service that has an optional path parameter (areaId, below). Setting the optional path parameter to null causes a NPE (exception below). The implications of this are that I have to create a different version of the method call for each optional call parameter and use lots of conditional logic to check if parameter values are null to decide which method to use. {code} String FIND_AREAS_QUERY = "/areas/{areaId}?countryId={countryId}&cityId={cityId}"; searchCriteria.setCountryID(123); searchCriteria.setCityID(345); searchCriteria.setAreaID(null); areas = restTemplate.getForObject( restEndpointUrl + FIND_AREAS_QUERY, Areas.class, searchCriteria.getAreaID(), /* NPE */ searchCriteria.getCountryID(), searchCriteria.getCityID() ); {code} The exception is as follows: {code} java.lang.NullPointerException at org.springframework.web.util.UriTemplate.expand(UriTemplate.java:128) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:400) at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:199) {code} The current workaround is below: This can get quite a burden for lots of optional parameters. {code} String FIND_AREAS_QUERY = "/areas/{areaId}?countryId={countryId}&cityId={cityId}"; searchCriteria.setCountryID(123); searchCriteria.setCityID(345); searchCriteria.setAreaID(null); if (searchCriteria.getAreaID() != null) { areas = restTemplate.getForObject( restEndpointUrl + FIND_AREAS_QUERY, Areas.class, searchCriteria.getAreaID(), searchCriteria.getCountryID(), searchCriteria.getCityID() ); } else { areas = restTemplate.getForObject( restEndpointUrl + FIND_AREAS_QUERY, Areas.class, searchCriteria.getCountryID(), searchCriteria.getCityID() ); } {code}

    Spring JIRA | 7 years ago | chris snow
    java.lang.NullPointerException
  2. 0

    I am trying to call a rest service that has an optional path parameter (areaId, below). Setting the optional path parameter to null causes a NPE (exception below). The implications of this are that I have to create a different version of the method call for each optional call parameter and use lots of conditional logic to check if parameter values are null to decide which method to use. {code} String FIND_AREAS_QUERY = "/areas/{areaId}?countryId={countryId}&cityId={cityId}"; searchCriteria.setCountryID(123); searchCriteria.setCityID(345); searchCriteria.setAreaID(null); areas = restTemplate.getForObject( restEndpointUrl + FIND_AREAS_QUERY, Areas.class, searchCriteria.getAreaID(), /* NPE */ searchCriteria.getCountryID(), searchCriteria.getCityID() ); {code} The exception is as follows: {code} java.lang.NullPointerException at org.springframework.web.util.UriTemplate.expand(UriTemplate.java:128) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:400) at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:199) {code} The current workaround is below: This can get quite a burden for lots of optional parameters. {code} String FIND_AREAS_QUERY = "/areas/{areaId}?countryId={countryId}&cityId={cityId}"; searchCriteria.setCountryID(123); searchCriteria.setCityID(345); searchCriteria.setAreaID(null); if (searchCriteria.getAreaID() != null) { areas = restTemplate.getForObject( restEndpointUrl + FIND_AREAS_QUERY, Areas.class, searchCriteria.getAreaID(), searchCriteria.getCountryID(), searchCriteria.getCityID() ); } else { areas = restTemplate.getForObject( restEndpointUrl + FIND_AREAS_QUERY, Areas.class, searchCriteria.getCountryID(), searchCriteria.getCityID() ); } {code}

    Spring JIRA | 7 years ago | chris snow
    java.lang.NullPointerException

    Root Cause Analysis

    1. java.lang.NullPointerException

      No message provided

      at org.springframework.web.util.UriTemplate.expand()
    2. Spring
      RestTemplate.getForObject
      1. org.springframework.web.util.UriTemplate.expand(UriTemplate.java:128)
      2. org.springframework.web.client.RestTemplate.execute(RestTemplate.java:400)
      3. org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:199)
      3 frames