Fala ae galera, blz?
Seguinte, tenho duas tabelas:
TABLE PROJETOS
id_projetos
titulo
resumo
id_professores
TABLE COMPONENTES_PROJETOS
id_componentes_projetos
id_alunos
id_projetos
Tenho um form que quero inserir as primeiras 3 informações na tabela PROJETOS, e uma parte na tabela COMPONENTES_PROJETOS que seria uma lista multipla com varios nomes....
Segue meu código abaixo, eu estou conseguindo inserir as 3 primeiras informacoes na tabela PROJETOS, mas nao consigo inserir a lista multipla na outra tabela...
Por favor me ajudem e ve o que estou fazendo de errado, ou oq ta faltando!
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:40)
br.com.scb.action.CadastraProjetosAction.execute(CadastraProjetosAction.java:21)
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)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.35 logs.
cadastrar-projetos.jsp
<form action="cadastraProjetos" method="post" id="valid" class="mainForm">
<div class="widget first">
<div class="head"><h5 class="iList">Cadastrar Projetos</h5></div>
<div class="rowElem nobg">
<label>Título:</label>
<div class="formRight">
<input type="text" name="projetos.titulo"/>
</div><div class="fix"></div>
</div>
<div class="rowElem">
<label>Resumo:</label>
<div class="formRight">
<textarea rows="8" cols="" name="projetos.resumo"></textarea>
</div>
<div class="fix"></div>
</div>
<div class="rowElem nobg">
<label>Professor Orientador:</label>
<div class="formRight">
<select name="projetos.id_professores" >
<option value="opt1">Selecione um orientador</option>
<c:forEach var="professor" items="${dao.lista}">
<option value="${professor.id_professores}">${professor.professor}</option>
</c:forEach>
</select>
</div>
<div class="fix"></div>
</div>
<div class="rowElem dualBoxes">
<h6>Componentes do Projeto</h6><br />
<div class="floatleft w40">
<select id="box1View" multiple="multiple" class="multiple" style="height:200px;">
<c:forEach var="aluno" items="${dao_aluno.lista}">
<option value="${aluno.id_alunos}">${aluno.nome}</option>
</c:forEach>
</select>
<br/>
<span id="box1Counter" class="countLabel"></span>
<div class="displayNone"><select id="box1Storage"></select></div>
</div>
<div class="floatleft dualControl">
<button id="to2" type="button" class="dualBtn mr5 mb15"> > </button>
<button id="allTo2" type="button" class="dualBtn"> >> </button><br />
<button id="to1" type="button" class="dualBtn mr5"> < </button>
<button id="allTo1" type="button" class="dualBtn"> << </button>
</div>
<div class="floatright w40">
<select name="projetos.id_alunos" id="box2View" multiple="multiple" class="multiple" style="height:240px;"></select><br/>
<span id="box2Counter" class="countLabel"></span>
<div class="displayNone"><select id="box2Storage"></select></div>
</div>
<div class="fix"></div>
</div>
<!--FIM COMPONENTE-->
<input type="submit" value="Cadastrar" class="basicBtn submitForm mb22" />
<div class="fix"></div>
</div>
</form>
CadastraProjetosAction.java
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.Projetos;
import br.com.scb.dao.ProjetosDAO;
public class CadastraProjetosAction extends ActionSupport {
private static final long serialVersionUID = -5817485575269317513L;
private Projetos projetos;
@Action( value="cadastraProjetos", results = {
@Result(name="ok", location="/cadastrar-projetos.jsp")
})
public String execute(){
new ProjetosDAO().adiciona(projetos);
addActionMessage("Projeto cadastrado com sucesso!");
return "ok";
}
public Projetos getProjetos() {
return projetos;
}
public void setProjetos(Projetos projetos) {
this.projetos = projetos;
}
}
ProjetosDAO
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();
}
//ADICIONAR Projetos
public void adiciona(Projetos projetos) {
String sql = "INSERT INTO projetos (titulo, resumo, id_professores) values (?,?,?)";
PreparedStatement stmt;
try {
stmt = connection.prepareStatement(sql);
stmt.setString(1, projetos.getTitulo());
stmt.setString(2, projetos.getResumo());
stmt.setInt(3, projetos.getId_professores());
stmt.execute();
} catch (SQLException e) {
throw new RuntimeException(e);
}
String sqlComponentes = "INSERT INTO componentes_projetos (id_alunos, id_projetos) values (?,?)";
PreparedStatement stmtComponentes;
for(Componentes componente : projetos.getComponentes()){
try {
stmtComponentes = connection.prepareStatement(sqlComponentes);
stmtComponentes.setInt(1, componente.getId_alunos());
stmtComponentes.setInt(2, projetos.getId_projetos());
stmtComponentes.execute();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
Projetos.java
package br.com.scb.modelo;
import java.util.List;
public class Projetos {
private int id_projetos;
private String titulo;
private String resumo;
private int id_professores;
private List<Componentes> componentes;
public List<Componentes> getComponentes() {
return componentes;
}
public void setComponentes(List<Componentes> componentes) {
this.componentes = componentes;
}
public int getId_projetos() {
return id_projetos;
}
public void setId_projetos(int id_projetos) {
this.id_projetos = id_projetos;
}
public String getTitulo() {
return titulo;
}
public void setTitulo(String titulo) {
this.titulo = titulo;
}
public String getResumo() {
return resumo;
}
public void setResumo(String resumo) {
this.resumo = resumo;
}
public int getId_professores() {
return id_professores;
}
public void setId_professores(int id_professores) {
this.id_professores = id_professores;
}
}
Componentes.java
package br.com.scb.modelo;
public class Componentes {
private int id_componentes_projetos;
private String componentes_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 String getComponentes_projetos() {
return componentes_projetos;
}
public void setComponentes_projetos(String componentes_projetos) {
this.componentes_projetos = componentes_projetos;
}
}