<pre><code>restTemplate.postForEntity(url,entity, String.class);
ResponseEntity&lt;String&gt; response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
</code></pre>

<p>Throws null pointer exception when trying to create an object.</p>

<p>I checked entity and url, it is getting printed in Logger message. But at this line it throws null pointer exception, but the object is still getting created..</p>

<p>If the object is getting created how can this throw null pointer exception..</p>

<p>I am using kubernetes, when i check command line in kubernetes it says object got created..but in logs it shows null pointer exception</p>

<p>The problem seem to be that you are executing the request <strong>two times</strong>.</p>

<pre><code>restTemplate.postForEntity(url, entity, String.class);
ResponseEntity&lt;String&gt; response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
</code></pre>

<p>Both <code>postForEntity</code> and <code>exchange</code> are sending a post request to your <code>url</code>. <code>postForEntity</code> can be seen as a specific case for the <code>exchange</code> method. See the <a href="https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html#postForEntity-java.net.URI-java.lang.Object-java.lang.Class-" rel="nofollow noreferrer">documentation</a></p>

<p>Please use only one of them, for example: </p>

<pre><code>ResponseEntity&lt;String&gt; response = restTemplate.postForEntity(url, entity, String.class);
</code></pre>
