I have <p:autoComplete>
working, and I have a commandButton
that opens dialog
so I can make a new one. The dialog
opens, I fill in the fields however at the time of saving this exception is thrown:
SEVERE: java.lang.NullPointerException at com.ouvidoria.bean.Converter.getAsObject Subject (SubjectConverter.java:31)
The error happens in the class I use to do the conversion so I can use autoComplete
. I believe it is so because clicking on the save button occurred an ajax request and as this field was empty the error occurred.
When I put a process="@this"
in the button save the error to but the object that I fill becomes null
, for example if in imputText
I put value="ouvidoriaBean.ouvidoria.telefone"
at the time I click on save this value it arrives null
. What could be happening and how can I solve this problem?
Save button
<p:commandButton value="Salvar"
style="margin-left:28%;margin-top:30px;width:45%"
actionListener="#{ouvidoriaBean.salvarOuvidoria}" update=":frmPrin" />
Converter Class
@FacesConverter("assunto")
public class AssuntoConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
System.out.println("CODIGO: " + value);
Integer codigo = null;
try {
System.out.println("getAsObject: " + value);
codigo = Integer.valueOf(value);
} catch (NumberFormatException e) {
}
List<Assunto> lista = new ArrayList<>();
AssuntoDAO assuntoDAO = new AssuntoDAO();
lista = assuntoDAO.listar();
for (Assunto a : lista) {
if (codigo.equals(a.getCodigo())) {
return a;
}
}
return null;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (value != null && !value.equals("")) {
Assunto assunto = (Assunto) value;
System.out.println("Testinho: " + assunto);
return String.valueOf(assunto.getCodigo());
}
return null;
}
}
Method of autoComplete
that stays in Bean:
public List<Assunto> sugerirAssunto(String consulta) {
List<Assunto> assuntosSugeridos = new ArrayList<>();
List<Assunto> listaAssunto = new ArrayList<>();
try {
AssuntoDAO assuntoDAO = new AssuntoDAO();
listaAssunto = assuntoDAO.listar();
for (Assunto assunto : listaAssunto) {
if (assunto.getAssunto().toLowerCase().startsWith(consulta.toLowerCase())) {
assuntosSugeridos.add(assunto);
}
}
} catch (Exception e) {
FacesUtil.adicionarMsgErro("Erro ao listar Assuntos: " + e.getMessage());
}
return assuntosSugeridos;
}
Auto Complete:
<p:autoComplete id="assunto" value="#{ouvidoriaBean.assunto}"
completeMethod="#{ouvidoriaBean.sugerirAssunto}"
forceSelection="true" converter="assunto" var="a"
itemLabel="#{a.assunto}" itemValue="#{a}" />