我正在使用带有Primefaces 3.4.2的JSF 2.0,当我运行我的应用程序时,我收到以下异常
java.lang.NullPointerException: Argument Error: Parameter text is null
at com.sun.faces.renderkit.html_basic.HtmlResponseWriter.writeText(HtmlResponseWriter.java:877)
at org.primefaces.component.datatable.DataTableRenderer.encodeFilter(DataTableRenderer.java:450)
at org.primefaces.component.datatable.DataTableRenderer.encodeColumnHeader(DataTableRenderer.java:360)
at org.primefaces.component.datatable.DataTableRenderer.encodeThead(DataTableRenderer.java:551)
at org.primefaces.component.datatable.DataTableRenderer.encodeRegularTable(DataTableRenderer.java:218)
at org.primefaces.component.datatable.DataTableRenderer.encodeMarkup(DataTableRenderer.java:192)
at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:74)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
JSF Code Snippet
<p:column id="empName" headerText="Name" filterBy="#{emp.empName}"
filterOptions="#{myMB.empFilter}"
filterMatchMode="exact">
<h:outputText value="#{emp.empName}"/>
</p:column>
在我的ManagedBean中
public Map<String,String> loadNames() {
names = new LinkedHashMap<String,String>();
names.put("Smith", "S");
names.put("Jones", "J");
return names;
}
private SelectItem[] empFilter;
public void init() {
empFilter= new SelectItem[loadNames().size() + 1];
empFilter[0] = new SelectItem("", "Select");
for (int i=1; i< empFilter.length; i++) {
empFilter[i] = new SelectItem(loadNames().get(i-1), loadNames().get(i-1));
}
}
答案 0 :(得分:4)
从loadNames()
进入底部循环,您无法正确获取值。
示例:
Map<String,String> map = new LinkedHashMap<String,String>();
map.put("Smith", "S");
map.put("Jones", "J");
System.out.println(map.get(0));
将返回null
。
你需要改变你的循环:
public void init()
{
empFilter= new SelectItem[loadNames().size() + 1];
empFilter[0] = new SelectItem("", "Select");
int i = 1;
for(Map.Entry<String,String> entry : loadNames().entrySet())
{
empFilter[i++] = new SelectItem(entry.getKey(),entry.getValue());
System.out.println(entry.getKey() + "/" + entry.getValue());
}
}