0
On a JSF page about a car shop, I first select the brand of the car and then choose the model. However, when selecting an item from the first "Hyundai" menu, it gives NullPointerException
and I can’t understand it. I’ve searched a lot, I’m a beginner in JSF.
UPDATED ( JA NO BEAN MANEGED COMING NULL). After I get out of the way Query() of the DAO the Selected tag is null when I selected the other menu to get the value related to the first menu "markerSelected" (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 the 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 you are 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)
Related, I recommend reading to simplify your DAO: http://answall.com/a/172910/132
– Victor Stafusa
Put a breakpoint in the method of loading the templates and see if it is triggered when selecting the brand.
– Roknauta
Which line launches the Exception? You have debugged to see if the parameter
marcaSelecionada
comes filled correctly? It is not null?– igventurelli
@Douglas is triggered yes, and comes from Nell Pointer in the Dão method for fetching modekos brandSelected.getName on this line to be exact.
– Rodrigo Costa
@igorventurelli already debugged tagSelected comes null in MB and In DAO also when I call dialSelected.getName in model query
– Rodrigo Costa
Post the complete Managedbean code, please.
– igventurelli
@igorventurelli Igor for this select one menu only uses these two methods so do not put all.
– Rodrigo Costa
Post the get&set of attributes that have relation then, please.
– igventurelli
@igorventurelli ta there would be only those involved
– Rodrigo Costa
Thanks! Try to take the
event="change"
from the ajax, please.– igventurelli
@igorventurelli had already done this, tried again and nothing.
– Rodrigo Costa
In the Bean, in the variable declaration, incise it with a new instance. Please.
private Marca marcaSelecionada = new Marca();
and in the methodcarregarModelos()
make aSystem.out.println(marcaSelecionada);
before consulting the bank.– igventurelli
When initializing with a new instance it gives the error Jan 11, 2017 10:20:37 AM with.sun.faces.application.view.Faceletviewhandlingstrategy handleRenderException Grave: Error Rendering View[/telaDeVendas.xhtml] java.lang.Classcastexception: Entity.Brand cannot be cast to Entity.Car
– Rodrigo Costa
https://github.com/rodrigoscsx/LojaDeCarros The project here is with source and html
– Rodrigo Costa