NullPointerException when executing same action multiple times - jsf

In my xhtml page, I have a search button and a p:datatable to display the search results. I added a c:if condition to hide and display the datatable.
<c:if test="#{!search_bean.isResultList}">
In my managedBean, I created the flag isResultEmpty and set it to true in my doInit(). In the action of my search button (actSearchForData), I've set it to true if my List is not empty.
private String actSearchForData() throws Exception {
if(resultList.size > 0) {
isResultEmpty = false;
}
}
Now this works without any errors the first time I execute actSearchForData and my resultList is displayed. But when I try to run actSearchForData the second time, I encounter nullpointer exception. I've tried debugging by returning isResultEmpty = true after getting the resultList but this only causes my flag to always return isResultEmpty = true.
How can I execute my search function multiple times and display the results in the datatable without getting any nullpointer exceptions?
UPDATE: I've tried using render again this time for the flag like rendered="!#{search_bean.isResultEmpty}". I no longer get nullpointerexception and my result list count displays the correct number of results but my data table does not show.

The JSF way to control conditional rendering is to use the rendered attribute like
<p:dataTable id="..."
value="#{search_bean.resultList}"
var="..."
rendered="#{not empty search_bean.resultList}" />
This will not render the datatable if resultList is null or resultList.size() is 0.

you must be getting NullPointerException (maybe) - because your "resultList" is null and still you are executing "resultList.size" inside "if(..)" statement
try something like This...
private String actSearchForData() {
if(resultList==null || resultList.size>0) {
isResultEmpty=false;
}
}
This code executes "isResultEmpty=false" - if(resultList is null)
Hope this works for you...

Related

Display popup when there are unsved inputs on dropdown change in JSF

How to display of popup when any of the two dropdowns selection changed and input values are modified using JSF. I am using valuechangelistener.
I have added a flag in my Mbean, if any of the input changes, this flag will be true. on change of the value of the dropdown, if this flag is true I need to show popup, but this is not coming
If you're using the<f:ajax/> tag inside of your dropdowns the following code snippet (especially the "onevent" attribute) might be useful:
<f:ajax render="itemsDataTable"
onevent="function(data) { if (data.status === 'success') {
alert('it works') ;} }"/>

JSF: Clear the DataTable when no records found?

I have a strange issue. I have a search criteria and i have a datatable which displays the results.
The search functionality works fine. Below is the issue.
I enter some search criteria i click on search button the records get displayed. Fine.
Again i enter some search criteria for which there are no records the datatable should get cleared.But the issue is the old record itself is getting displayed. What can be the problem
Below is my java code:
public void results(TestItem[] TestItemTOArray, TestForm TestForm, boolean isNewSearch, CarService carService) throws Exception {
if (viewBean instanceof TestViewBean) {
TestViewBean = (TestViewBean) viewBean;
}
TestRowForm[] TestRowFormArray = this.convertToTestRowForm(TestItemTOArray, TestForm, isNewSearch, carService);
if(TestRowFormArray.length > 0)
TestForm.setTestRowForm(TestRowFormArray);
if(TestRowFormArray!=null){
TestForm.setTestRowForm(TestRowFormArray);
TestForm.setTotalRows(TestRowFormArray.length);
}
}
JSF:
<rich:dataTable
value="#{testController.testForm.testRowForm}"
rows="#{testController.noOfRecordsToShow}" var="resultRow" noDataLabel="No Result Found"
headerClass="ColumnHeader" styleClass="DataTable" rowKeyVar="itc"
style="width:980px;" id="dtTable">
You should clean up data source for data table in case when result set is empty too.

JPA JSF Assign value to a null object

Good Afternoon in my timezone.
I am working on a web application, that uses both JSF and JPA.
There is one screen that contains multiple checkBoxes , something like this:
option1 check1 check2
option2 check1 check2
option3 check1 check2
In the database the query to retrieve the values of the checkboxes uses one LEFT JOIN (this is business logic) to retrieve the checkbox values:
Select * FROM Option LEFT OUTER JOIN CheckBoxType ....
The class diagram is something like this:
class Option
String label
checkBoxType checkBox
class CheckBoxType
boolean value;
The final result is , it could exist a list of "Option" in which some contains the checkBox variable to NULL.
In the JSF we use the following code:
<ui:repeat var="assocCheck" value="#{checkBean.getOptions}">
<h:selectBooleanCheckBox name="#{assocCheck.label}" value="#{assocCheck.checkBox.value}"/>
</ui:repeat>
To present the results there is no problem, in the case of the "Option" objects that contains the checkBox to null the checkbox on the screen appers with NO value , when i set to true one of this checkboxes and try to save them, the JSF throws a exception telling me the following
javax.faces.component.UpdateModelException: javax.el.PropertyNotFoundException: ....xhtml #45,126 value="#{assocCheck.checkBox.value}": Target Unreachable, 'null' returned null
I understand why this is happening , what is the best manner to deal with a problem like this ?Could adding a valueChangeListener on the checkbox(every time a checkbox is full filled there is a method on the bean that creates the checkBox object) be the best approach ?
Thanks in advance
Best regards
I would use a lazy setter / getter in your Option class:
#Entity
public class Option {
#...ToOne(optional = true, ...)
private CheckboxType checkbox;
public boolean isCheckboxChecked() {
return checkbox != null && checkbox.getValue();
}
public void setCheckboxChecked(boolean checked) {
if (checkbox == null) {
if (!checked) {
// No need to create a CheckboxType
return;
}
checkbox = new CheckboxType();
}
checkbox.setValue(checked);
}
}
Now you can use that one in your JSF page:
<h:selectBooleanCheckBox name="#{option.label}" value="#{option.checkBoxChecked}"/>
Why I would use this approach: It keeps the business logic attached to the business object. Especially the implicit statement checkbox == null means the same as checkbox.value == false is made explicit.
BTW: I would rename Checkbox.value to Checkbox.checked - from my point of view it makes things clearer: checkbox.isChecked() vs. checkbox.getValue() (or even worse checkbox.isValue()).

How to get Trinidad tr:inputText to echo its changed value instantly?

I have the following Trinidad tr:inputText field in my xhtml file that is bound to "value1" in my managed bean class; the inputText field is of type:
org.apache.myfaces.trinidad.component.core.input.CoreInputText
<tr:inputText value="#{myBean.value1}" autoSubmit="true" immediate="true" valueChangeListener="#{myBean.textChangeListener}" styleClass="stylePageTextSmallColored">
</tr:inputText>
<tr:commandButton id="btnCommit" inlineStyle="width:20" immediate="true" text="Commit" action="#{myBean.doCommit()}" styleClass="styleButton">
</tr:commandButton>
public void textChangeListener(ValueChangeEvent e) {
log.debug(">> textChangeListener: value=["+(String)e.getNewValue()+"]");
}
public String doCommit() throws Throwable {
log.debug("In doCommit: value1="+value1);
value1 = "("+value1+")";
// I update my database with the modified value1 string here; the database has the correct
// updated value1 string (with the parentheses).
// How do I get the modified value1 (above) to echo back to the screen instantly
// inside the doCommit() method with its changes?
}
When I type into this field and change its value then press the "commit" button, I can see its value properly obtained from the screen in the doCommit() method and the textChangeListener() method indicates that it has been called. I would like to make some changes to the "value1" variable and echo its value back instantly to the screen without doing another screen submit; can this be done as part of the doCommit() method or through some other mechanism/tag in the xhtml file?
Please note that this is using Trinidad and the input text class is listed above.
Update...
Thank you for the reference Jasper; I want the SAME inputText field to update itself after the setter method is called and the input text is changed (by the setter method). Thanks to the reference page that you pointed to; I tried the following and it works by using the partialTriggers attribute:
<tr:inputText id="myValue" value="#{myBean.value1}" autoSubmit="true" immediate="true" partialTriggers="myValue" valueChangeListener="#{myBean.textChangeListener}" styleClass="stylePageTextSmallColored"> </tr:inputText>
You can do so using Trinidad's Partial Page Rendering.
In short: you need to add an id attribute to your input, and have and tr:outputText component with a partialTriggers attribute where you refer to the input component.
Minimal implementation in your case would be:
<tr:inputText id="value1Input"
value="#{myBean.value1}"
autoSubmit="true"
immediate="true"/>
<tr:outputText value="value1: #{myBean.value1}"
partialTriggers="value1Input"/>

NullPointerException while displaying the same page after some taks in jsf

First I want to tell :
I saw How to use JSF's h:selectBooleanCheckbox with h:dataTable to create one object per row?
and my problem is like same but not solved problem.
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
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.

Resources