title: Do i need to catch NullPointerException in rest controller?
body: <p>I know NPE is an unchecked (RTE), and as such we need to prevent it from happening rather than catching it: <a href="https://www.javaworld.com/article/2072719/effective-java-nullpointerexception-handling.html" rel="nofollow noreferrer">https://www.javaworld.com/article/2072719/effective-java-nullpointerexception-handling.html</a></p>
  
  <p>But still, let's say I have a REST controller that returns an employee record upon a request. That depends on finding the underlying employee record in the DB before doing update. See the example below:</p>
  
  <pre><code>@RequestMapping(value = BENEFIT_VERSION, method = GET)
  @PreAuthorize(READ_COMPANY_DATA)
  public ResponseEntity get(@PathVariable UUID employerId,
                                @PathVariable Long versionId) {
      log.info("Request to fetch benefit version, employerId: {}, versionId: {}", employerId, versionId);
  
      BenefitVersion benefitVersion = benefitVersionService.findByEmployerIdAndId(employerId, versionId); &lt;--- Here NPE may be thrown
  
      if (benefitVersion == null) {
          log.warn("Unable to fetch non-existent benefit version, employerId: %s, ID: %s", employerId, versionId);
          return new ResponseEntity(NOT_FOUND);
      }
  
      RestBenefitVersion restBenefitVersion = restBenefitVersionAssemblerFacade.toResource(benefitVersion);
  
      return new ResponseEntity&lt;&gt;(restBenefitVersion, OK);
  }
  </code></pre>
  
  <p>The <code>benefitVersionService.findByEmployerIdAndId(employerId, versionId)</code> bit may throw NPE. </p>
  
  <p>There is no handler for the NPE associated with this controller, so NPE will simply kill the request processing. Is that all right or would be better to provide a NPE handler for this case to return a meaningful response to the client instead?</p>