Results 1 to 10 of 10
- 04-04-2012, 05:09 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 9
- Rep Power
- 0
Problem with Select Multiple Struts2
I have a multiple select list in, I would write to the database, but is returning null!
Componentes.javaJava Code:type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception java.lang.NullPointerException br.com.scb.dao.ProjetosDAO.adiciona(ProjetosDAO.java:76) br.com.scb.action.CadastraProjetosAction.execute(CadastraProjetosAction.java:24) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) java.lang.reflect.Method.invoke(Unknown Source) com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:453) com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:292) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:255) org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265) org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:211) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:211) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:90) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:192) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54) org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:510) org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
Java Code:package br.com.scb.modelo; public class Componentes { private int id_componentes_projetos; private int[] id_alunos; private int id_projetos; public int getId_componentes_projetos() { return id_componentes_projetos; } public void setId_componentes_projetos(int id_componentes_projetos) { this.id_componentes_projetos = id_componentes_projetos; } public int[] getId_alunos() { return id_alunos; } public void setId_alunos(int[] id_alunos) { this.id_alunos = id_alunos; } public int getId_projetos() { return id_projetos; } public void setId_projetos(int id_projetos) { this.id_projetos = id_projetos; } }
JSP
When I view the html part of the form is as follows:Java Code:<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>SCB - Painel de Administração</title> <%@ include file="header.jsp" %> </head> <body> <%@ include file="top.jsp" %> <!-- Content wrapper --> <div class="wrapper"> <%@ include file="menu.jsp" %> <!--MELHORAR A VALIDAÇÃO AQUI--> <div class="success message"> <s:if test="hasActionMessages()"> <s:actionmessage/> </s:if> </div> <jsp:useBean id="dao" class="br.com.scb.dao.ProfessoresDAO" /> <jsp:useBean id="dao_alunos" class="br.com.scb.dao.AlunosDAO" /> <!-- Content --> <div class="content"> <div class="title"><h5>Projetos</h5></div> <div class="widget first"> <div class="head"><h5 class="iList">Cadastrar Projetos</h5></div> <div class="rowElem dualBoxes"> <h6>Componentes do Projeto</h6><br /> <div class="floatleft w40"> <s:form action="cadastraProjetos" method="post" id="valid" styleClass="mainForm"> <s:select id="box1View" cssClass="multiple" cssStyle="height:200px;" list="%{#attr.dao_alunos.lista}" listKey="id_alunos" listValue="nome" name="id_alunos" multiple="true" size="%{#attr.dao_aluno.lista.size()}"></s:select> <s:submit value="Cadastrar" styleClass="basicBtn submitForm mb22"></s:submit> </s:form> <br/> </div> <div class="fix"></div> </div> <!--FIM COMPONENTE--> <!--input type="submit" value="Cadastrar" class="basicBtn submitForm mb22" / --> <div class="fix"></div> </div> </div> <div class="fix"></div> </div> <!-- Footer --> <%@ include file="footer.jsp" %> </body> </html>
CadastraProjetosAction.javaJava Code:<div class="floatleft w40"> <form id="valid" name="valid" action="/scb/cadastraProjetos.action" method="post" styleClass="mainForm"> <table class="wwFormTable"> <tr> <td class="tdLabel"></td> <td ><select name="id_alunos" size="2" id="box1View" class="multiple" style="height:200px;" multiple="multiple"> <option value="15">Thiago</option> <option value="16">Andrei Ribeiro</option> </select> <input type="hidden" id="__multiselect_box1View" name="__multiselect_id_alunos" value="" /> </td> </tr> <tr> <td colspan="2"><div align="right"><input type="submit" id="valid_0" value="Cadastrar" styleClass="basicBtn submitForm mb22"/> </div></td> </tr> </table></form> <br/> </div>
ProjetosDAO.javaJava Code:package br.com.scb.action; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Result; import com.opensymphony.xwork2.ActionSupport; import br.com.scb.modelo.Componentes; import br.com.scb.dao.ProjetosDAO; public class CadastraProjetosAction extends ActionSupport { private static final long serialVersionUID = -5817485575269317513L; private Componentes componentes; @Action( value="cadastraProjetos", results = { @Result(name="ok", location="/cadastrar-projetos.jsp") }) public String execute(){ new ProjetosDAO().adiciona(componentes); addActionMessage("Projeto cadastrado com sucesso!"); return "ok"; } public Componentes getComponentes() { return componentes; } public void setComponentes(Componentes componentes) { this.componentes = componentes; } }
Java Code:package br.com.scb.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import br.com.scb.jdbc.ConnectionFactory; import br.com.scb.modelo.Componentes; import br.com.scb.modelo.Projetos; public class ProjetosDAO { private final Connection connection; public ProjetosDAO(){ connection = new ConnectionFactory().getConnection(); } //ADD PROJECTS public void adiciona(Componentes componentes) { String sql = "INSERT INTO componentes_projetos (id_alunos, id_projetos) values (?,?)"; PreparedStatement stmt; //Projetos projetos = new Projetos(); try { stmt = connection.prepareStatement(sql); stmt.setInt(1, componentes.getId_alunos()); stmt.setInt(2, 1); stmt.execute(); } catch (SQLException e) { throw new RuntimeException(e); } } }
- 04-04-2012, 09:35 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Problem with Select Multiple Struts2
What is null?
What line is that exception being thrown from?
One comment, though not about this problem.
You never close your connections.Please do not ask for code as refusal often offends.
- 04-04-2012, 12:20 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 9
- Rep Power
- 0
Re: Problem with Select Multiple Struts2
The object componente return null!!
Thanks
- 04-04-2012, 01:02 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Problem with Select Multiple Struts2
All I can suggest is sticking some breakpoints in there and debugging it, just to ensure that Struts isn't calling setComponentes() for example.
Oh, hang on.
IS the name for your select list correct?
I'm pretty sure that should be componentes.id_alunos, otherwise how does Struts know where to stick the values selected?Please do not ask for code as refusal often offends.
- 04-04-2012, 02:59 PM #5
Member
- Join Date
- Apr 2012
- Posts
- 9
- Rep Power
- 0
Re: Problem with Select Multiple Struts2
Hi friend, I put as you said:
<s:select name="componentes.id_alunos" id="box1View" cssClass="multiple" cssStyle="height:200px;" list="%{#attr.dao_alunos.lista}" listKey="id_alunos" listValue="nome" multiple="true" size="%{#attr.dao_alunos.lista.size()}"></s:select>
Now I can record only one selected, when I select it from more than one error.
I tried putting a breakpoint to see what comes in the components, but the error appears before the breakpoint!
Java Code:04/04/2012 09:54:07 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn AVISO: Error setting expression 'componentes.id_alunos' with value '[Ljava.lang.String;@1489c06' ognl.MethodFailedException: Method "setId_alunos" failed for object br.com.scb.modelo.Componentes@1fb7cbb [java.lang.NoSuchMethodException: br.com.scb.modelo.Componentes.setId_alunos([Ljava.lang.String;)] at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1292) at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:1481) at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:85) at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:162) at com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.setProperty(ObjectAccessor.java:27) at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2318) at ognl.ASTProperty.setValueBody(ASTProperty.java:127) at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220) at ognl.SimpleNode.setValue(SimpleNode.java:301) at ognl.ASTChain.setValueBody(ASTChain.java:227) at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220) at ognl.SimpleNode.setValue(SimpleNode.java:301) at ognl.Ognl.setValue(Ognl.java:737) at com.opensymphony.xwork2.ognl.OgnlUtil.setValue(OgnlUtil.java:217) at com.opensymphony.xwork2.ognl.OgnlValueStack.trySetValue(OgnlValueStack.java:186) at com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:173) at com.opensymphony.xwork2.ognl.OgnlValueStack.setParameter(OgnlValueStack.java:151) at com.opensymphony.xwork2.interceptor.ParametersInterceptor.setParameters(ParametersInterceptor.java:292) at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:203) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:211) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:90) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:192) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54) at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:510) at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Unknown Source) Caused by: java.lang.NoSuchMethodException: br.com.scb.modelo.Componentes.setId_alunos([Ljava.lang.String;) at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1233) ... 64 more /-- Encapsulated exception ------------\ java.lang.NoSuchMethodException: br.com.scb.modelo.Componentes.setId_alunos([Ljava.lang.String;)
- 04-04-2012, 03:43 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Problem with Select Multiple Struts2
That's because it's trying to populate it with an array of Strings.
Not sure how you get it to know that it should be parsing those as ints.
You might need to check through the Struts docs (and I know they're not exactly great).Please do not ask for code as refusal often offends.
- 04-04-2012, 04:35 PM #7
Member
- Join Date
- Apr 2012
- Posts
- 9
- Rep Power
- 0
Re: Problem with Select Multiple Struts2
but at what time I have to do this?
parseInt does it work?
thanks
- 04-04-2012, 04:55 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Problem with Select Multiple Struts2
No, you need to find out from the Struts documentation how it expects to handle an int[] like this.
I'd have to go through the docs myself to sort it out, so you may as well do that instead of me.Please do not ask for code as refusal often offends.
- 04-04-2012, 10:18 PM #9
Member
- Join Date
- Apr 2012
- Posts
- 9
- Rep Power
- 0
Re: Problem with Select Multiple Struts2
package br.com.scb.modelo;
public class Componentes {
private int id_componentes_projetos;
private int[] id_alunos;
private int id_projetos;
public int getId_componentes_projetos() {
return id_componentes_projetos;
}
public void setId_componentes_projetos(int id_componentes_projetos) {
this.id_componentes_projetos = id_componentes_projetos;
}
public int[] getId_alunos() {
return id_alunos;
}
public void setId_alunos(String[] id_alunos) {
String[] valoresStr = id_alunos;
Integer[] valoresInt = new Integer[valoresStr.length];
}
public int getId_projetos() {
return id_projetos;
}
public void setId_projetos(int id_projetos) {
this.id_projetos = id_projetos;
}
}
I managed to convert String to int, but I have a difficulty.
What I have to return to use the variable in ComponentesDAO.java?
Why in ComponentesDAO, I will make the FOR for recording data.
Example:
Thanks friend!Java Code:for (int i = 0; i < valoresStr.length; i++) { valoresInt[i] = Integer.parseInt(valoresStr[i]); }Last edited by franksniper; 04-04-2012 at 10:23 PM.
- 04-05-2012, 09:23 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Similar Threads
-
Select multiple files in IE
By kirtichopra in forum Advanced JavaReplies: 3Last Post: 02-03-2012, 06:33 AM -
Struts2 java.lang.reflect.InvocationTargetException (Struts2 with Hibernate)
By sivakumar_sakam in forum Web FrameworksReplies: 0Last Post: 10-30-2011, 03:38 PM -
problem with checkbox in struts2
By sunithamm in forum Web FrameworksReplies: 8Last Post: 03-15-2011, 11:56 AM -
struts2 tags problem
By videanuadrian in forum New To JavaReplies: 0Last Post: 12-10-2010, 10:47 AM -
Struts2.0 Error messages problem
By Sayed in forum Advanced JavaReplies: 0Last Post: 12-03-2008, 11:38 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks