Eu vacilei feio kkk não estava chamando o método do DAO que faz a query de select la no BD… mas agora deu um erro. Veja ai:
package view;
import controller.CadastrosContaController;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import model.ContaDAO;
public class CadastroContaView extends javax.swing.JFrame {
public CadastroContaView() {
initComponents();
this.setLocationRelativeTo(null);
jTFConta.setDocument(new Numeral());
jTFSaldo.setDocument(new Numeral());
jTFLimite.setDocument(new Numeral());
getRootPane().setDefaultButton(botaoInserir);
}
CadastrosContaController contasController = new CadastrosContaController();
ViewTable tabela = new ViewTable();
ContaDAO dao = new ContaDAO();
/**
* 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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jPanel3 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jTFConta = new javax.swing.JTextField();
jTFNome = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jTFSaldo = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
jTFLimite = new javax.swing.JTextField();
botaoInserir = new javax.swing.JButton();
botaoLimpar = new javax.swing.JButton();
botaoVisualizar = new javax.swing.JButton();
botaoSair = new javax.swing.JButton();
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("CADASTRO");
setMinimumSize(new java.awt.Dimension(328, 462));
jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Cadastro de Contas", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 3, 18))); // NOI18N
jPanel3.setPreferredSize(new java.awt.Dimension(300, 100));
jLabel1.setText("Conta:");
jTFConta.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusLost(java.awt.event.FocusEvent evt) {
jTFContaFocusLost(evt);
}
});
jTFConta.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTFContaActionPerformed(evt);
}
});
jTFNome.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTFNomeActionPerformed(evt);
}
});
jLabel3.setText("Titular:");
jLabel2.setText("Saldo: ");
jTFSaldo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTFSaldoActionPerformed(evt);
}
});
jLabel4.setText("Limite: ");
botaoInserir.setText("INSERIR");
botaoInserir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
botaoInserirActionPerformed(evt);
}
});
botaoLimpar.setText("LIMPAR");
botaoLimpar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
botaoLimparActionPerformed(evt);
}
});
botaoVisualizar.setText("VISUALIZAR");
botaoVisualizar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
botaoVisualizarActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addComponent(botaoInserir)
.addGap(10, 10, 10)
.addComponent(botaoVisualizar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
.addComponent(botaoLimpar))
.addGroup(jPanel3Layout.createSequentialGroup()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jTFConta, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTFNome, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTFSaldo, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4)
.addComponent(jTFLimite, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3)
.addComponent(jLabel2))
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTFConta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTFNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(12, 12, 12)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTFSaldo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTFLimite, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(25, 25, 25)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(botaoInserir)
.addComponent(botaoLimpar)
.addComponent(botaoVisualizar))
.addContainerGap(78, Short.MAX_VALUE))
);
botaoSair.setText("SAIR");
botaoSair.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
botaoSairActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(137, 137, 137)
.addComponent(botaoSair)))
.addContainerGap(20, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(47, 47, 47)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 362, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(botaoSair)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void botaoLimparActionPerformed(java.awt.event.ActionEvent evt) {
limpar();
}
private void botaoInserirActionPerformed(java.awt.event.ActionEvent evt) { // Aqui seria o botão inserir, onde insiro no DB os dados, mandando aqui para o controller. (Este processo está funcionando perfeitamente.)
contasController.insereDados(Integer.parseInt(jTFConta.getText()),
jTFNome.getText(),
Double.parseDouble(jTFSaldo.getText()),
Double.parseDouble(jTFLimite.getText()));
limpar();
}
private void jTFContaActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jTFSaldoActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jTFNomeActionPerformed(java.awt.event.ActionEvent evt) {
}
private void botaoSairActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void botaoVisualizarActionPerformed(java.awt.event.ActionEvent evt) { // Botão Visualizar que chama a outra tela:
tabela.setVisible(true);
dao.read(); // No final deste código estará o método read da classe DAO.
}
private void jTFContaFocusLost(java.awt.event.FocusEvent evt) {
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(CadastroContaView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(CadastroContaView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(CadastroContaView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(CadastroContaView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CadastroContaView().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton botaoInserir;
private javax.swing.JButton botaoLimpar;
private javax.swing.JButton botaoSair;
private javax.swing.JButton botaoVisualizar;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel3;
private javax.swing.JTextField jTFConta;
private javax.swing.JTextField jTFLimite;
private javax.swing.JTextField jTFNome;
private javax.swing.JTextField jTFSaldo;
// End of variables declaration
public void limpar() {
jTFConta.setText("");
jTFNome.setText("");
jTFSaldo.setText("");
jTFLimite.setText("");
jTFConta.requestFocus();
}
class Numeral extends PlainDocument {
@Override
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
int tamanho = (this.getLength() + str.length());
if (tamanho <= 7) {
super.insertString(offs, str.replaceAll("[aA-zZ]", ""), a);
} else {
super.insertString(offs, str.replaceAll("[aA0-zZ9]", ""), a);
}
}
}
}
Agora o método dao.read()
, citado no botão Visualizar
public void read() {
PreparedStatement state;
ResultSet rs;
try {
state = ConnectionFactory.getConnection().prepareStatement(SQLBUSCAR);
rs = state.executeQuery();
while (rs.next()) {
conta.setConta(rs.getInt(1)); // Agora quando executo, da um NullPointerException nessa linha aqui.
conta.setNome(rs.getString(2));
conta.setSaldo(rs.getDouble(3));
conta.setLimite(rs.getDouble(4));
contasBD.add(conta);
controller.preencheList(contasBD); // chama o método do controller, já postado anteriormente.
}
} catch (SQLException ex) {
System.err.println("Erro: " + ex);
}
}