java.lang.NullPointerException

There are no available Samebug tips for this exception. Do you have an idea how to solve this issue? A short tip would help users who saw this issue last week.

  • 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}
    via by chris snow,
  • 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}
    via by chris snow,
    • 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)
    No Bugmate found.