Primefaces 自动补全器中的NullPointerException
我使用下一个Java API以编程方式创建自动完成组件:Primefaces 自动补全器中的NullPointerException,primefaces,autocomplete,nullpointerexception,Primefaces,Autocomplete,Nullpointerexception,我使用下一个Java API以编程方式创建自动完成组件: public class AutoCompleteHandler extends UIComponentHandler<AutoComplete> { @Override public AutoComplete handle(AutoComplete component, Map<String, String> attributes) { FacesContext ctx = FacesContext.ge
public class AutoCompleteHandler extends UIComponentHandler<AutoComplete> {
@Override
public AutoComplete handle(AutoComplete component, Map<String, String> attributes) {
FacesContext ctx = FacesContext.getCurrentInstance();
Application application = ctx.getApplication();
component.setId(attributes.get(InspectionResultConstants.NAME));
String elExpression = FacesUtils.wrapExpression("addressFinder.getAddressList");
MethodExpression methodExpression = application.getExpressionFactory().createMethodExpression(ctx.getELContext(), elExpression, List.class, new Class<?>[]{String.class});
component.setCompleteMethod(methodExpression);
return component;
}
我不确定你和我是否有同样的问题。当我的bean被ViewScoped注释时,我得到了NullPointerException。在我更改为RequestScope后,一切似乎都正常。我在使用update=“@form”时在p:autocomplete中遇到了相同的异常,然后我更改为update=“componentA componentB”并再次工作。希望能有帮助。
@Named
@ViewScoped
public class AddressFinder implements Serializable {
private String addressValue;
private ArrayList<String> selections = new ArrayList<>();
private String uIComponentName;
public String getAddressValue() {
return addressValue;
}
public void setAddressValue(String addressValue) {
this.addressValue = addressValue;
}
public void setUIComponentName(String name) {
this.uIComponentName = name;
}
public List<String> getAddressList(String searchStr) throws SQLException {
AddressFinderUtils.recalculateSelections(selections, searchStr);
Client client = AddressFinderUtils.getClient();
int phase = selections.size();
String idexName = AddressFinderUtils.getESIndexNameBySearchPhase(phase, this.uIComponentName);
if (StringUtils.isEmpty(idexName)) return null;
String typeName = AddressFinderUtils.getESTypeNameBySearchPhase(phase, this.uIComponentName);
String esQuery = AddressFinderUtils.getQueryBySearchPashe(phase, selections, searchStr, this.uIComponentName);
SearchResponse response = client.prepareSearch(idexName)
.setTypes(typeName)
.setExtraSource(esQuery)
.setFetchSource(true).setFrom(0).setSize(10)
.execute()
.actionGet();
String ESSourceFieldName = AddressFinderUtils.getESSorceFieldNameBySearchPhase(phase, this.uIComponentName);
if (ESSourceFieldName == null) return null;
List<String> results = new ArrayList<>();
String resStr;
for (SearchHit searchHit : response.getHits()) {
resStr = searchHit.sourceAsMap().get(ESSourceFieldName).toString();
results.add(resStr);
}
return results;
}
public void onItemSelect(SelectEvent event) {
selections.add(event.getObject().toString());
}
}
Primefaces version is 5.2