Yubaraj
Yubaraj

Reputation: 3988

NullPointerException while displaying the same page after some taks in jsf

First I want to tell :

Following is my code :

*.xhtml

<rich:column>
     <h:selectBooleanCheckbox  id="resolveTxn" value="#{verifyTransactionBean.checked[verifyTxnList.id]}"/>
</rich:column>
.
.//Some code here
.
<h:commandButton value="Resolve" action="#{verifyTransactionBean.resolveToTxn}"  styleClass="btn" />

and following is my code

private Map<Long, Boolean> checked = new HashMap<Long, Boolean>();

public void resolveToTxn() {
    List<ToVerifyTxnDTO> list = new ArrayList<ToVerifyTxnDTO>();
    for (ToVerifyTxnDTO dTO : toVerifyTxnDTOList) {
        if(checked.get(dTO.getId())){//I got null pointer exception here
           Long id=dTO.getId(); 
           verifyTransactionSessionBeanLocal.updateResolvedflag(id);
        }
    }

}

What I want to do ?

I want to pass checked row id as parameter on following :

verifyTransactionSessionBeanLocal.updateResolvedflag(id);

And after clicking the button Resolve some operation should be done and refresh dataTable and display same page. Operation is done but getting null pointer exception while displaying (Reddering) the same page

Thanks

Upvotes: 0

Views: 228

Answers (1)

BalusC
BalusC

Reputation: 1109715

The if () test on a Boolean will throw NullPointerException if the Boolean itself is actually null instead of true or false. In other words, the checked.get(dTO.getId()) has unexpectedly returned null for some reason.

That can have several causes:

  • The JSF implementation hasn't properly updated the checked map with the value from <h:selectBooleanCheckbox> for some reason.
  • The EL implementation was using Boolean instead of boolean for some reason.
  • The toVerifyTxnDTOList has incompatibly changed during the process which caused that the currently iterated dTO isn't the one which was been displayed in the table at the moment checkboxes were clicked (perhaps because the bean is request scoped instead of view scoped).
  • The toVerifyTxnDTOList contains more items than present in the checked map because you're using pagination and displaying only a subset at once.

In any case, to fix the NullPointerException, just add an additional nullcheck:

Boolean dtoChecked = checked.get(dTO.getId());

if (dtoChecked != null && dtoChecked) {
    // ...
}

This however doesn't fix the underlying cause of your problem. My best guess would be that the toVerifyTxnDTOList contains more items than actually being displayed in the table. Thus, it's one of the last mentioned two causes mentioned in the list. The first mentioned two causes are theoretically possible, but in practice I haven't seen this before.

Upvotes: 1

Related Questions