title: Hibernate - Query Result contains null and throws expection
body: <p>I have two tables, TBL_MARKET_WATCH_NAME (<strong>Master</strong>) and TBL_MARKET_WATCH (<strong>Details</strong>) connected through an MW_ID column in MySQL. I'm using hibernate and doing a LEFT JOIN on these two tables to fetch data. Most of the time the <strong>Details</strong> table may not have data for the connecting ID column from left table which results in null values in resultset.</p>
  <p><strong>Master</strong> table is defined as</p>
  <pre><code>@Entity(name = &quot;TBL_MARKET_WATCH_NAME&quot;)
  
  @Id
  @GeneratedValue
  @Column(name = &quot;ID&quot;)
  private Long id;
  
  @OneToMany(cascade = CascadeType.ALL)
  @JoinColumn(name = &quot;MW_ID&quot;)
  private List&lt;MarketWatchScripDetailsDTO&gt; mwDetailsDTO;
  
  @Column(name = &quot;MW_ID&quot;)
  private Integer mwId;
  
  @Column(name = &quot;USER_ID&quot;)
  private String userId;
  
  @Column(name = &quot;MW_NAME&quot;)
  private String mwName;
  </code></pre>
  <p>The <strong>Details</strong> table is defined as below</p>
  <pre><code>@Entity(name = &quot;TBL_MARKET_WATCH&quot;)
  
  @Id
  @GeneratedValue
  @Column(name = &quot;ID&quot;)
  private Long id;
  
  @Column(name = &quot;USER_ID&quot;)
  private String userId;
  
  @Column(name = &quot;MW_ID&quot;)
  private Integer mwId;
  
  @Column(name = &quot;SCRIP_NAME&quot;)
  private String scripName;
  </code></pre>
  <p>In <strong>Master</strong> table, I have data like below</p>
  <p><a href="https://i.stack.imgur.com/MfhlB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MfhlB.png" alt="enter image description here" /></a></p>
  <p>In <strong>Details</strong> table, I have data as below</p>
  <p><a href="https://i.stack.imgur.com/Hl4sx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Hl4sx.png" alt="enter image description here" /></a></p>
  <p>I have a left join query running on Hibernate as below</p>
  <pre><code>@Transactional
  @Query(value = &quot;SELECT A.mwName as mwName, B.userId as userId, B.mwId as mwId, B.scripName as scripName FROM TBL_MARKET_WATCH_NAME as A LEFT JOIN A.mwDetailsDTO B on  A.mwId = B.mwId and A.userId = B.userId where A.userId = :pUserId&quot;)
  </code></pre>
  <p>When running the query and converting it into an object my resultset is as below</p>
  <pre><code>    List&lt;Object&gt; getUserScripDetailsNew(@Param(&quot;pUserId&quot;) String pUserId);
  
    [
      &quot;LIST0&quot;,
      &quot;650156&quot;,
      1,
      &quot;NATURALGAS 26OCT22 FUT&quot;,
    ],
    .... &lt;***some more data***&gt;
    [
      &quot;LIST2&quot;,
      null,
      null,
      null
    ]
  </code></pre>
  <p>Instead of <em><strong>List</strong></em> if I use model interface like the one defined below, I'm getting null pointer exception because it is not able to convert null values to integer.</p>
  <pre><code>List&lt;MwTblResponseModel&gt; getUserScripDetailsNew(@Param(&quot;pUserId&quot;) String pUserId);
  
  public interface MwTblResponseModel {
    public String getMwName();
  
    public String getUserId();
  
    public Integer getMwId();
  
    public String getScripName();
  </code></pre>
  <p>}</p>
  <p>The error is as below.</p>
  <pre><code>java.lang.NullPointerException
  at in.codifi.marketwatch.model.MwTblResponseModel_e3fe8bfde9ba7f8b10664ce55c6c5ef842396ab4.convert_getUserScripDetailsNew(Unknown Source)
  at in.codifi.marketwatch.dao.repos.MarketWatchNameRepository_486524f74fd05ac96e0dcf8fffaa2af5a4b95b43Impl$$function$$1.apply(Unknown Source)
  at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
  at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
  at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
  at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
  at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
  at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
  at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
  at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
  at org.hibernate.query.spi.StreamDecorator.collect(StreamDecorator.java:209)
  at in.codifi.marketwatch.dao.repos.MarketWatchNameRepository_486524f74fd05ac96e0dcf8fffaa2af5a4b95b43Impl.getUserScripDetailsNew(Unknown Source)
  at in.codifi.marketwatch.dao.repos.MarketWatchNameRepository_486524f74fd05ac96e0dcf8fffaa2af5a4b95b43Impl_Subclass.getUserScripDetailsNew$$superforward1(Unknown Source)
  at in.codifi.marketwatch.dao.repos.MarketWatchNameRepository_486524f74fd05ac96e0dcf8fffaa2af5a4b95b43Impl_Subclass$$function$$8.apply(Unknown Source)
  at io.quarkus.arc.impl.AroundInvokeInvocationContext.proceed(AroundInvokeInvocationContext.java:53)
  at io.quarkus.arc.runtime.devconsole.InvocationInterceptor.proceed(InvocationInterceptor.java:62)
  at io.quarkus.arc.runtime.devconsole.InvocationInterceptor.monitor(InvocationInterceptor.java:49)
  at io.quarkus.arc.runtime.devconsole.InvocationInterceptor_Bean.intercept(Unknown Source)
  at io.quarkus.arc.impl.InterceptorInvocation.invoke(InterceptorInvocation.java:41)
  at io.quarkus.arc.impl.AroundInvokeInvocationContext.perform(AroundInvokeInvocationContext.java:40)
  at io.quarkus.arc.impl.InvocationContexts.performAroundInvoke(InvocationContexts.java:32)
  at in.codifi.marketwatch.dao.repos.MarketWatchNameRepository_486524f74fd05ac96e0dcf8fffaa2af5a4b95b43Impl_Subclass.getUserScripDetailsNew(Unknown Source)
  </code></pre>
  <p>How to make sure when null values are coming from Query to assign 0 in final response?</p>