E ae pessoal, tudo bom!!?
Eu estou aprendendo a mexer no Netbeans, isso é, fuçar né… hehehe
Ë muito legal, facinho de montar os esquemas… mas quando tento colcoar alguma coisas que inclua banco de dados, da pau… e pior que nem dapara sabe rque pau que é… é fogo… ele me seta uma linha que não tem explicação e da um erro de NullPoiterException… o erro é este:
[code]java.lang.NullPointerException
at cadastro.dao.CadastroDB.save(CadastroDB.java:33)
at cadastro.CadastroEndereco.insertCadastroDB(CadastroEndereco.java:254)
at cadastro.CadastroEndereco.access$000(CadastroEndereco.java:15)
at cadastro.CadastroEndereco$1.actionPerformed(CadastroEndereco.java:160)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1839) ...[/code]
e por ae vai…
vou colocar as 3 classes aqui para vocês verem:
que erro absurdo é esse… funciona direitinho de uum outro jeito…
[code]package cadastro.dao;
import java.sql.*;
/**
*
-
@author br_1004
*/
public class ConnectionManager {public static final String STR_DRIVER = "com.mysql.jdbc.Driver";
private static final String NOME_BANCO = "cadastroendereco";
public static final String STR_CON = "jdbc:mysql://192.168.1.90:3306/"+NOME_BANCO+"?user=root&password=root";
private static final String USUARIO = "root";
private static final String SENHA = "root";
public static String teste = "";public Connection getConexao() {
Connection conn = null; try { Class.forName(STR_DRIVER).newInstance(); conn = DriverManager.getConnection(STR_CON); System.out.println("Conectado"); return conn; } catch (ClassNotFoundException e) { String errorMsg = "Driver nao encontrado"; } catch (SQLException e) { String errorMsg = "Erro ao obter conexão"; } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn;
}
public void close(Connection conn) {
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
String errorMsg = "Nao foi possivel dfechar a conexao com o banco";
}
}public void close(Connection conn, Statement stmt) {
try { if (stmt != null) { stmt.close(); } } catch (Exception e) { System.out.println("Não foi possível fechar a cnexão com o banco"); e.printStackTrace(); } close(conn);
}
public void close(Connection conn, Statement stmt, ResultSet rs) {
try { if (rs != null) { rs.close(); } } catch (Exception e) { System.out.println("Não foi possível fechar a cnexão com o banco"); e.printStackTrace(); } close(conn, stmt);
}
}
[/code]
[code]package cadastro.dao;
import java.sql.*;
import cadastro.beans.Cadastro;
/**
*
-
@author br_1004
*/
public class CadastroDB {public void save(Cadastro cadastro) {
ConnectionManager cm = new ConnectionManager();Connection conn = cm.getConexao(); Statement stmt; String sql = "INSERT INTO cadastro (nome,sobrenome,endereco,numero,"; sql += " bairro,cidade,uf,fone,cel,fax) "; sql += " VALUES ('"+cadastro.getNome()+"','"+cadastro.getSobrenome()+"'"; sql += " ,'"+cadastro.getEndereco()+"',"+cadastro.getNumero(); sql += " ,'"+cadastro.getBairro()+"','"+cadastro.getCidade()+"'"; sql += " ,'"+cadastro.getUf()+"','"+cadastro.getFone()+"'"; sql += " ,'"+cadastro.getCel()+"','"+cadastro.getFax()+"')"; System.out.println(ConnectionManager.teste); try { stmt = conn.createStatement(); stmt.executeUpdate(sql); System.out.println("SQL = " + sql); System.out.println("Produto inserido no banco de dados" + cadastro); } catch (SQLException e) { System.out.println("Erro de sql!!!!!"); e.printStackTrace(); } finally { cm.close(conn); }
}
}[/code]
[code]package cadastro;
import cadastro.dao.;
import cadastro.beans.;
/**
*
-
@author br_1004
*/
public class CadastroEndereco extends javax.swing.JFrame {/** Creates new form CadastroEndereco */
public CadastroEndereco() {
initComponents();
}/** This method is called from within the constructor to
-
initialize the form.
-
WARNING: Do NOT modify this code. The content of this method is
-
always regenerated by the Form Editor.
*/
private void initComponents() {
Label = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
enderecoTextField = new javax.swing.JTextField();
nomeTextField = new javax.swing.JTextField();
sobrenomeTextField = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
bairroTextField = new javax.swing.JTextField();
cidadeTextField = new javax.swing.JTextField();
ufTextField = new javax.swing.JTextField();
foneTextField = new javax.swing.JTextField();
jLabel8 = new javax.swing.JLabel();
celTextField = new javax.swing.JTextField();
jLabel9 = new javax.swing.JLabel();
faxTextField = new javax.swing.JTextField();
jLabel10 = new javax.swing.JLabel();
numeroTextField = new javax.swing.JTextField();
jLabel11 = new javax.swing.JLabel();
jLabel12 = new javax.swing.JLabel();
incluirButton = new javax.swing.JButton();
sairButton = new javax.swing.JButton();
resetarButton1 = new javax.swing.JButton();
menuBar = new javax.swing.JMenuBar();
fileMenu = new javax.swing.JMenu();
openMenuItem = new javax.swing.JMenuItem();
saveMenuItem = new javax.swing.JMenuItem();
saveAsMenuItem = new javax.swing.JMenuItem();
exitMenuItem = new javax.swing.JMenuItem();
editMenu = new javax.swing.JMenu();
cutMenuItem = new javax.swing.JMenuItem();
copyMenuItem = new javax.swing.JMenuItem();
pasteMenuItem = new javax.swing.JMenuItem();
deleteMenuItem = new javax.swing.JMenuItem();
helpMenu = new javax.swing.JMenu();
contentsMenuItem = new javax.swing.JMenuItem();
aboutMenuItem = new javax.swing.JMenuItem();getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(204, 204, 204));
Label.setFont(new java.awt.Font("Verdana", 1, 10));
Label.setIcon(new javax.swing.ImageIcon("C:\Meus documentos\Programas\Messenger\emoticons\good.jpg"));
Label.setText("Cadastro Endere\u00e7os");
getContentPane().add(Label, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, -1));jLabel1.setFont(new java.awt.Font("Verdana", 0, 10));
jLabel1.setText("Nome:");
getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 50, -1, 20));jLabel2.setFont(new java.awt.Font("Verdana", 0, 10));
jLabel2.setText("Sobrenome:");
getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 50, -1, 20));jLabel3.setFont(new java.awt.Font("Verdana", 0, 10));
jLabel3.setText("Endere\u00e7o:");
getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 90, -1, 20));jLabel4.setFont(new java.awt.Font("Verdana", 0, 12));
jLabel4.setText("________________________________________________");
getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, -1, -1));enderecoTextField.setText("endereco");
getContentPane().add(enderecoTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 90, 190, -1));nomeTextField.setText("nome");
getContentPane().add(nomeTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 50, 120, -1));sobrenomeTextField.setText("sobrenome");
getContentPane().add(sobrenomeTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 50, 140, -1));jLabel5.setFont(new java.awt.Font("Verdana", 0, 10));
jLabel5.setText("Bairro:");
getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 130, -1, 20));jLabel6.setFont(new java.awt.Font("Verdana", 0, 10));
jLabel6.setText("Cidade:");
getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 130, -1, 20));jLabel7.setFont(new java.awt.Font("Verdana", 0, 10));
jLabel7.setText("UF:");
getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 130, -1, 20));bairroTextField.setText("bairro");
getContentPane().add(bairroTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 130, 130, -1));cidadeTextField.setText("cidade");
getContentPane().add(cidadeTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 130, 90, -1));ufTextField.setText("uf");
getContentPane().add(ufTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 130, 30, -1));foneTextField.setText("fone");
getContentPane().add(foneTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 170, 80, -1));jLabel8.setFont(new java.awt.Font("Verdana", 0, 10));
jLabel8.setText("Fone:");
getContentPane().add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 170, -1, 20));celTextField.setText("cel");
getContentPane().add(celTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 170, 90, -1));jLabel9.setFont(new java.awt.Font("Verdana", 0, 10));
jLabel9.setText("Cel:");
getContentPane().add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 170, -1, 20));faxTextField.setText("fax");
getContentPane().add(faxTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(297, 170, 90, -1));jLabel10.setFont(new java.awt.Font("Verdana", 0, 10));
jLabel10.setText("Fax:");
getContentPane().add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 170, -1, 20));numeroTextField.setText("numero");
getContentPane().add(numeroTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(317, 90, 70, -1));jLabel11.setFont(new java.awt.Font("Verdana", 0, 10));
jLabel11.setText("Numero:");
getContentPane().add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 90, -1, 20));jLabel12.setFont(new java.awt.Font("Verdana", 0, 12));
jLabel12.setText("________________________________________________");
getContentPane().add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 200, -1, -1));incluirButton.setFont(new java.awt.Font("Verdana", 0, 10));
incluirButton.setText("Incluir");
incluirButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
insertCadastroDB(evt);
}
});getContentPane().add(incluirButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 220, -1, -1));
sairButton.setFont(new java.awt.Font("Verdana", 0, 10));
sairButton.setText("Sair");
sairButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fechar(evt);
}
});getContentPane().add(sairButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 220, -1, -1));
resetarButton1.setFont(new java.awt.Font("Verdana", 0, 10));
resetarButton1.setText("resetar");
resetarButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
resetaTextFelds(evt);
}
});getContentPane().add(resetarButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 220, 80, -1));
fileMenu.setText("File");
openMenuItem.setText("Open");
fileMenu.add(openMenuItem);saveMenuItem.setText("Save");
fileMenu.add(saveMenuItem);saveAsMenuItem.setText("Save As …");
fileMenu.add(saveAsMenuItem);exitMenuItem.setText("Exit");
exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitMenuItemActionPerformed(evt);
}
});fileMenu.add(exitMenuItem);
menuBar.add(fileMenu);
editMenu.setText("Edit");
cutMenuItem.setText("Cut");
editMenu.add(cutMenuItem);copyMenuItem.setText("Copy");
editMenu.add(copyMenuItem);pasteMenuItem.setText("Paste");
editMenu.add(pasteMenuItem);deleteMenuItem.setText("Delete");
editMenu.add(deleteMenuItem);menuBar.add(editMenu);
helpMenu.setText("Help");
contentsMenuItem.setText("Contents");
helpMenu.add(contentsMenuItem);aboutMenuItem.setText("About");
helpMenu.add(aboutMenuItem);menuBar.add(helpMenu);
setJMenuBar(menuBar);
pack();
}
private void fechar(java.awt.event.ActionEvent evt) {
System.exit(0);
}private void insertCadastroDB(java.awt.event.ActionEvent evt) {
Cadastro cadastro = new Cadastro();
cadastro.setNome(this.nomeTextField.getText());
cadastro.setSobrenome(this.sobrenomeTextField.getText());
cadastro.setEndereco(this.enderecoTextField.getText());
cadastro.setNumero(Integer.parseInt(this.numeroTextField.getText()));
cadastro.setBairro(this.bairroTextField.getText());
cadastro.setCidade(this.cidadeTextField.getText());
cadastro.setUf(this.ufTextField.getText());
cadastro.setFone(this.foneTextField.getText());
cadastro.setCel(this.celTextField.getText());
cadastro.setFax(this.faxTextField.getText());CadastroDB cdb = new CadastroDB(); cdb.save(cadastro);
}
private void resetaTextFelds(java.awt.event.ActionEvent evt) {
this.nomeTextField.setText("");
this.sobrenomeTextField.setText("");
this.enderecoTextField.setText("");
this.numeroTextField.setText("");
this.bairroTextField.setText("");
this.cidadeTextField.setText("");
this.ufTextField.setText("");
this.foneTextField.setText("");
this.celTextField.setText("");
this.faxTextField.setText("");
}private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}/**
-
@param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CadastroEndereco().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel Label;
private javax.swing.JMenuItem aboutMenuItem;
private javax.swing.JTextField bairroTextField;
private javax.swing.JTextField celTextField;
private javax.swing.JTextField cidadeTextField;
private javax.swing.JMenuItem contentsMenuItem;
private javax.swing.JMenuItem copyMenuItem;
private javax.swing.JMenuItem cutMenuItem;
private javax.swing.JMenuItem deleteMenuItem;
private javax.swing.JMenu editMenu;
private javax.swing.JTextField enderecoTextField;
private javax.swing.JMenuItem exitMenuItem;
private javax.swing.JTextField faxTextField;
private javax.swing.JMenu fileMenu;
private javax.swing.JTextField foneTextField;
private javax.swing.JMenu helpMenu;
private javax.swing.JButton incluirButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel12;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JMenuBar menuBar;
private javax.swing.JTextField nomeTextField;
private javax.swing.JTextField numeroTextField;
private javax.swing.JMenuItem openMenuItem;
private javax.swing.JMenuItem pasteMenuItem;
private javax.swing.JButton resetarButton1;
private javax.swing.JButton sairButton;
private javax.swing.JMenuItem saveAsMenuItem;
private javax.swing.JMenuItem saveMenuItem;
private javax.swing.JTextField sobrenomeTextField;
private javax.swing.JTextField ufTextField;
// End of variables declaration -
}
[/code]
Ai meu Deus… o que será que acuntece…
VAlws pessoal!!
T+!!