1
I have a <p:autoComplete>
working, and I have a commandButton
that opens a dialog
so that I can make a new registration. The dialog
opens, I fill the fields but when saving this exception is launched:
GRAVE: java.lang.Nullpointerexception at com.ouvidoria.bean.AssuntoConverter.getAsObject(Assuntoconverter.java:31)
The error happens in the class I use to convert to autoComplete
. I believe it is so because when clicking the save button occurred an ajax request and as this field was empty the error occurred.
When I put a process="@this"
on the button save the error to however the object I fill is null
, for example if in the imputText
place the value="ouvidoriaBean.ouvidoria.telefone"
when I click to save this value arrives null
. What may be happening and how I can 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
which is 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}" />
Thank you so much for helping @Wellington Avelino
– DiegoAugusto
@Techies
– Wellington Avelino