In a JSF page about a car shop, first select the car brand and then choose the model. However, when selecting an item from the first menu "Hyundai", it gives NullPointerException
and I can not understand. I've tried a lot, I'm a beginner in JSF.
UPDATED (JA NO MANEGED BEAN COMING NULL). After you leave the method Query () of the DAO the selectedSelected is already null, in the case when I selected the other menu was to get the value related to the first menu "Selected brand" (Hyundai) and see which models available showing in menu2.
View:
<h:form id="frmVendas">
<p:outputPanel id="venderCarro" />
<h:panelGrid id="painel" columns="1">
<p:outputLabel for="comboMarca" value="Marcas*: " />
<p:selectOneMenu id="comboMarca" converter="conversorObjetoId" value="#{vendasMB.marcaSelecionada}">
<f:selectItem itemLabel="--Selecione--" itemValue="" />
<f:selectItems value="#{vendasMB.listaMarca}" var="marca" itemLabel="#{marca.nome}" itemValue="#{marca}" />
<p:ajax event="change" update="comboModelos" listener="#{vendasMB.carregarModelos}" />
</p:selectOneMenu>
</h:panelGrid>
<h:panelGrid id="painel2" columns="1">
<p:outputLabel for="comboModelos" value="Modelos*: " />
<p:selectOneMenu id="comboModelos" value="#{vendasMB.modeloSelecionado}" converter="conversorObjetoId">
<f:selectItem itemLabel="--Selecione--" itemValue="" />
<f:selectItems value="#{vendasMB.listaModelo}" var="carro" itemLabel="#{carro.modelo}" itemValue="#{carro}" />
</p:selectOneMenu>
</h:panelGrid>
</h:form>
Managed bean
Two methods for SelectOneMenu
private List<Carro> listaCarros = new ArrayList<Carro>();
private List<Marca> listaMarca = new ArrayList<Marca>();
private List<Carro> listaModelo = new ArrayList<Carro>();
private String carroSelecionado;
private Marca marcaSelecionada ;
private Carro modeloSelecionado;
@PostConstruct
public void init() {
listaMarca = carrosDAO.consultarMarca();
System.out.println("--> init*()");
}
public void carregarModelos() {
listaModelo = carrosDAO.consultarModelos(marcaSelecionada);
//return listaModelo;
}
DAO:
public List<Marca> consultarMarca() {
List<Marca> listaMarca = new ArrayList<Marca>();
StringBuilder sb = new StringBuilder();
Marca marca;
Integer idMarca = 0;
try {
Connection con;
Statement stmt = null;
PreparedStatement ps = null;
ResultSet rs = null;
String urloracle = "jdbc:oracle:thin:@172.30.0.25:1521:desv";
String useroracle = "rodrigoscs";
String passwordoracle = "regueiro";
OracleDataSource ds;
ds = new OracleDataSource();
ds.setURL(urloracle);
con = ds.getConnection(useroracle, passwordoracle);
stmt = con.createStatement();
sb.append("SELECT NOME, ID FROM MARCA ");
ps = con.prepareStatement(sb.toString());
rs = ps.executeQuery();
// percorre o resultado da consulta
while (rs.next()) {
marca = new Marca();
marca.setMarcaID(++idMarca);
marca.setNome(rs.getString("NOME"));
marca.setMarcaID(rs.getInt("ID"));
listaMarca.add(marca);
}
// fechando a connection
if (con != null) {
con.close();
con = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (rs != null) {
rs.close();
rs = null;
}
} catch (SQLException ex) {
System.out.println("Erro: " + ex.getMessage());
}
return listaMarca;
}
Method giving NullPointerException
:
public List<Carro> consultarModelos(Marca marcaSelecionada) {
List<Carro> listaModelo = new ArrayList<Carro>();
StringBuilder sb = new StringBuilder();
Carro carro;
Integer idCarro = 0;
try {
Connection con;
Statement stmt = null;
PreparedStatement ps = null;
ResultSet rs = null;
String urloracle = "jdbc:oracle:thin:@172.30.0.25:1521:desv";
String useroracle = "rodrigoscs";
String passwordoracle = "regueiro";
OracleDataSource ds;
ds = new OracleDataSource();
ds.setURL(urloracle);
con = ds.getConnection(useroracle, passwordoracle);
stmt = con.createStatement();
sb.append("SELECT C.CARROS_MODELO, M.NOME FROM CARROS C, MARCA M WHERE ID = CARROS_ID AND M.ID= ? ");
ps = con.prepareStatement(sb.toString());
ps.setInt(1, marcaSelecionada.getMarcaID());
rs = ps.executeQuery();
// percorre o resultado da consulta
while (rs.next()) {
carro = new Carro();
carro.setId(++idCarro);
carro.setModelo(rs.getString("CARROS_MODELO"));
listaModelo.add(carro);
}
// fechando a connection
if (con != null) {
con.close();
con = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (rs != null) {
rs.close();
rs = null;
}
} catch (SQLException ex) {
System.out.println("Erro: " + ex.getMessage());
}
return listaModelo;
}
get and set
public List<Marca> getLista() {
return listaMarca;
}
public void setLista(List<Marca> lista) {
this.listaMarca = lista;
}
public List<Marca> getListaMarca() {
return listaMarca;
}
public void setListaMarca(List<Marca> listaMarca) {
this.listaMarca = listaMarca;
}
public Marca getMarcaSelecionada() {
return marcaSelecionada;
}
public void setMarcaSelecionada(Marca marcaSelecionada) {
this.marcaSelecionada = marcaSelecionada;
}
public List<Carro> getListaModelo() {
return listaModelo;
}
public void setListaModelo(List<Carro> listaModelo) {
this.listaModelo = listaModelo;
}
public Carro getModeloSelecionado() {
return modeloSelecionado;
}
public void setModeloSelecionado(Carro modeloSelecionado) {
this.modeloSelecionado = modeloSelecionado;
}
}
ERROR:
- > init * ()
Jan 11, 2017 9:00:46 AM com.sun.faces.lifecycle.InvokeApplicationPhase execute
Advertência: /telaDeVendas.xhtml @21,107 listener="#{vendasMB.carregarModelos}": java.lang.NullPointerException
javax.el.ELException: /telaDeVendas.xhtml @21,107 listener="#{vendasMB.carregarModelos}": java.lang.NullPointerException
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:111)
at org.primefaces.component.behavior.ajax.AjaxBehaviorListenerImpl.processAjaxBehavior(AjaxBehaviorListenerImpl.java:53)
at javax.faces.event.AjaxBehaviorEvent.processListener(AjaxBehaviorEvent.java:113)
at javax.faces.component.behavior.BehaviorBase.broadcast(BehaviorBase.java:106)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:762)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:786)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1251)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:98)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NullPointerException
at DAO.CarrosDAO.consultarModelos(CarrosDAO.java:168)
at MB.VendasMB.carregarModelos(VendasMB.java:82)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
... 29 more
Jan 11, 2017 9:00:46 AM com.sun.faces.context.AjaxExceptionHandlerImpl handlePartialResponseError
Grave: java.lang.NullPointerException
at DAO.CarrosDAO.consultarModelos(CarrosDAO.java:168)
at MB.VendasMB.carregarModelos(VendasMB.java:82)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at org.primefaces.component.behavior.ajax.AjaxBehaviorListenerImpl.processAjaxBehavior(AjaxBehaviorListenerImpl.java:53)
at javax.faces.event.AjaxBehaviorEvent.processListener(AjaxBehaviorEvent.java:113)
at javax.faces.component.behavior.BehaviorBase.broadcast(BehaviorBase.java:106)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:762)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:786)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1251)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:98)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)