Galera sou novo aqui no forúm, e essa é meu primeiro post.
Então paciência!
Gente comecei a programar em JAVA com BD.
E tenho que fazer o cadastro de uma eleição.
Esse cadastro tem as seguintes funções:
“Cadastra, Atualiza e Remove” uma eleição.
E está dando esse erro filho da mãe. :?
Quando eu uso só as classes beans, executavel e a janela, fica com o eventos e a conexão do BD, funciona! :S’
Não sei mais o que eu faço. Espero que me ajudem.
E[color=red]xception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at bd.acesso.acessando(acesso.java:43)
at gui.Janela.actionPerformed(Janela.java:118)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)[/color]
Meu programa está a seguir:
Classe Janela.java do pacote gui
[code]
package gui;
import bean.beans;
import bd.acesso;
// E todos os outros importas necessários
public class Janela extends JFrame implements ActionListener{
private JLabel a1, c1, d1;
private JTextField a2, c2, d2;
private JButton a3, c3, r3, l3;
Statement stmt;
Connection con;
public Janela(){
setSize (500,300);
setTitle ("Cadastro de Eleição");
setLocation (250,200);
setResizable(false);
// Criação do JLabel "ANO"
a1 = new JLabel ("Ano: ");
a1.setForeground(Color.RED);
a1.setFont(new Font("Rockwell", Font.CENTER_BASELINE, 20));
// Criação do JLabel "Cargo"
c1 = new JLabel ("Cargo: ");
c1.setForeground(Color.RED);
c1.setFont(new Font("Rockwell", Font.CENTER_BASELINE, 20));
// Criação do JLabel "Descrição"
d1 = new JLabel ("Descrição: ");
d1.setForeground(Color.RED);
d1.setFont(new Font("Rockwell", Font.CENTER_BASELINE, 20));
// Criação do JTextField do Ano
a2 = new JTextField (5);
a2.setBackground(Color.black);
a2.setForeground(Color.RED);
a2.setFont(new Font("Rockwell", Font.BOLD, 15));
// Criação do JTextField do Cargo
c2 = new JTextField (30);
c2.setBackground(Color.black);
c2.setForeground(Color.RED);
c2.setFont(new Font("Rockwell", Font.BOLD, 15));
// Criação do JTexField da Descrição
d2 = new JTextField (40);
d2.setBackground(Color.black);
d2.setForeground(Color.RED);
d2.setFont(new Font("Rockwell", Font.BOLD, 15));
// Criação do botão "Cadastrar"
c3 = new JButton ("Cadastrar");
c3.setBackground(Color.BLACK);
c3.setFont(new Font("Rockwell", Font.BOLD, 15));
c3.setForeground(Color.RED);
c3.addActionListener(this);
// Criação do botão "Remover"
r3 = new JButton ("Remover");
r3.setBackground(Color.BLACK);
r3.setFont(new Font("Rockwell", Font.BOLD, 15));
r3.setForeground(Color.RED);
r3.addActionListener(this);
// Criação do botão "Limpar"
l3 = new JButton ("Limpar");
l3.addActionListener(this);
l3.setBackground(Color.BLACK);
l3.setFont(new Font("Rockwell", Font.BOLD, 15));
l3.setForeground(Color.RED);
// Criação do botão "Fechar"
a3 = new JButton ("Atualizar");
a3.setForeground(Color.red);
a3.setBackground(Color.BLACK);
a3.setFont(new Font("Rockwell", Font.BOLD, 15));
a3.addActionListener(this);
// Cria o Layout e Adiciona os botões
setLayout(new GridLayout(5,2));
getContentPane().setBackground(Color.BLACK);
add(a1);
add(a2);
add(c1);
add(c2);
add(d1);
add(d2);
add(c3);
add(r3);
add(a3);
add(l3);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
acesso bee = new acesso();
bee.acessando(e);
}
}[/code]
Classe beans do package bean
[code]package bean;
import javax.swing.JButton;
import javax.swing.JTextField;
public class beans {
private String ano, cargo, desc;
private JTextField a2, c2, d2;
private JButton a3,c3,l3,r3;
public void setAno(String ano) {
this.ano = ano;
}
public String getAno() {
return ano;
}
public void setCargo(String cargo) {
this.cargo = cargo;
}
public String getCargo() {
return cargo;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getDesc() {
return desc;
}
public void setA2(JTextField a2) {
this.a2 = a2;
}
public JTextField getA2() {
return a2;
}
public void setC2(JTextField c2) {
this.c2 = c2;
}
public JTextField getC2() {
return c2;
}
public void setD2(JTextField d2) {
this.d2 = d2;
}
public JTextField getD2() {
return d2;
}
public void setA3(JButton a3) {
this.a3 = a3;
}
public JButton getA3() {
return a3;
}
public void setC3(JButton c3) {
this.c3 = c3;
}
public JButton getC3() {
return c3;
}
public void setL3(JButton l3) {
this.l3 = l3;
}
public JButton getL3() {
return l3;
}
public void setR3(JButton r3) {
this.r3 = r3;
}
public JButton getR3() {
return r3;
}
}[/code]
A Classe Executavel do package exe
package exe;
import gui.Janela;
public class Executavel {
public static void main (String[] args){
Janela j = new Janela();
j.setVisible(true);
}
}
[size=18]E a classe que tá dando problema: Classe acesso do package BD![/size]
[code]package bd;
import bean.beans;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
public class acesso {
Connection con;
Statement stmt;
public void acessando(ActionEvent e) {
beans ee = new beans();
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/eleicao","root","123");
stmt = con.createStatement();
JOptionPane.showMessageDialog(null, "Conexão Feita com Sucesso","Conectou",JOptionPane.INFORMATION_MESSAGE);
if (e.getSource()==ee.getC3()){
eventoCadastrar();
}
else if (e.getSource()==ee.getR3()){
eventoRemover();
}
else if (e.getSource()==ee.getA3()) {
eventoAtualizar();
}
else {
ee.getA2().setText("");
ee.getD2().setText("");
ee.getC2().setText("");
}
con.close();
} catch (ClassNotFoundException e1) {
JOptionPane.showMessageDialog(null, e1.getMessage(),"Erro",JOptionPane.ERROR_MESSAGE);
} catch (SQLException e1) {
JOptionPane.showMessageDialog(null, e1.getMessage(),"Erro",JOptionPane.ERROR_MESSAGE);
} catch (NumberFormatException e1){
JOptionPane.showMessageDialog(null, "Caractere invalido","Erro",JOptionPane.ERROR_MESSAGE);
}
}
// Evento que realiza a Atualização
private void eventoAtualizar() throws SQLException {
beans ee = new beans();
ee.setAno(ee.getA2().getText());
ee.setCargo(ee.getC2().getText());
ee.setDesc(ee.getD2().getText());
int intAno;
String nomeAux = "";
if(ee.getAno().equals("")|| ee.getCargo().equals("")){
String mensagem = "Preencha todos os dados";
JOptionPane.showMessageDialog(null, mensagem,"Erro",JOptionPane.ERROR_MESSAGE);
}
else{
ResultSet rs = stmt.executeQuery("SELECT ano FROM cadastro where ano= '"+ee.getAno()+"'");
if(rs.next()){
nomeAux = rs.getString("ano");
}
if(nomeAux == ""){
JOptionPane.showMessageDialog(null, "Não houve eleição nesse ano", "ERRO!!", JOptionPane.ERROR_MESSAGE);
}
else{
intAno = Integer.parseInt(ee.getAno());
stmt.executeUpdate("UPDATE cadastro SET cargo = '"+ee.getCargo()+"', descricao = '"+ee.getDesc()+"' where ano = '"+intAno+"'");
JOptionPane.showMessageDialog(null,"Dados Atualizados com Sucesso","Sucesso",JOptionPane.INFORMATION_MESSAGE);
ee.getA2().setText(null);
ee.getC2().setText(null);
ee.getD2().setText(null);
}
}
}
// Evento que faz a remoção
private void eventoRemover() throws SQLException {
beans ee = new beans();
ee.setAno(ee.getA2().getText());
ee.setCargo(ee.getC2().getText());
ee.setDesc(ee.getD2().getText());
int intAno; String nomeAux = "";
if(ee.getAno().equals("")|| ee.getCargo().equals("")){
String mensagem = "Preencha todos os dados";
JOptionPane.showMessageDialog(null, mensagem,"Erro",JOptionPane.ERROR_MESSAGE);
}
else{
ResultSet rs = stmt.executeQuery("SELECT ano FROM cadastro where ano= '"+ee.getAno()+"'");
if(rs.next()){
nomeAux = rs.getString("ano");
}
if(nomeAux == ""){
JOptionPane.showMessageDialog(null, "Essa eleição não Existe", "ERRO!!", JOptionPane.ERROR_MESSAGE);
}
else{
intAno = Integer.parseInt(ee.getAno());
stmt.executeUpdate("DELETE from cadastro where ano = '"+intAno+"'");
JOptionPane.showMessageDialog(null,"Dados Removido com Sucesso","Sucesso",JOptionPane.INFORMATION_MESSAGE);
ee.getA2().setText(null);
ee.getC2().setText(null);
ee.getD2().setText(null);
}
}
}
// Evento que faz o cadastramento
public void eventoCadastrar() throws SQLException{
beans ee = new beans();
ee.setAno(ee.getA2().getText());
ee.setDesc(ee.getD2().getText());
ee.setCargo(ee.getC2().getText());
if(ee.getAno().equals("")|| ee.getDesc().equals("")|| ee.getCargo().equals("")){
String mensagem = "Preencha todos os dados";
JOptionPane.showMessageDialog(null, mensagem,"Erro",JOptionPane.ERROR_MESSAGE);
}
else{
int anoInt = Integer.parseInt(ee.getAno());
stmt.executeUpdate("INSERT into cadastro(ano, cargo, descricao)values('"+anoInt+"' , '"+ee.getCargo()+"' , '"+ee.getDesc()+"')");
JOptionPane.showMessageDialog(null,"Cadastro Concluído","Cadastro Concluido",JOptionPane.INFORMATION_MESSAGE);
ee.getA2().setText(null);
ee.getC2().setText(null);
ee.getD2().setText(null);
}
}
}[/code]