0
Apparently it is to be right, but when I run the following errors:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at bibl.PesquisaSimpl.preencheTabela(PesquisaSimpl.java:188)
at bibl.PesquisaSimpl.<init>(PesquisaSimpl.java:25)
at bibl.Bibl.bConsultaSimplActionPerformed(Bibl.java:241)
at bibl.Bibl.access$400(Bibl.java:12)
at bibl.Bibl$5.actionPerformed(Bibl.java:100)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
I didn’t find the problem, I wonder if someone could help me?
That is the code:
package bibl;
import java.awt.event.KeyEvent;
import java.util.Iterator;
import java.util.List;
import javax.swing.table.DefaultTableModel;
/**
*
* @author Kauan
*/
public class PesquisaSimpl extends javax.swing.JFrame {
private BiblCadastro cadastro = new BiblCadastro();
BdBibl bd;
/**
* Creates new form PesquisaSimpl
*/
public PesquisaSimpl() {
initComponents();
preencheTabela();
bd = new BdBibl();
setTitle("SIGE - Módulo Biblioteca [Consulta de Livros]");
}
/**
* 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() {
jLabel1 = new javax.swing.JLabel();
tTitulo = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
tAutor = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
tEditora = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
tCSimpl = new javax.swing.JTable();
bSair = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Título");
tTitulo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tTituloActionPerformed(evt);
}
});
jLabel2.setText("Autor");
jLabel3.setText("Editora");
jLabel4.setText("Obs.:Preencha o espaço e pressione ENTER.");
tCSimpl.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null}
},
new String [] {
"CDD/Cutter", "Título", "Autor", "Local", "Editora", "Ano", "Situação"
}
));
jScrollPane1.setViewportView(tCSimpl);
bSair.setText("Sair");
bSair.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bSairActionPerformed(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()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(tTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tAutor, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tEditora, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bSair))
.addComponent(jLabel4))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(16, 16, 16)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(tTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2)
.addComponent(tAutor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3)
.addComponent(tEditora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bSair))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void bSairActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.dispose();
}
private void tTituloActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
public void keyPressed(KeyEvent e){
int tecla = e.getKeyCode();
if (tecla == KeyEvent.VK_ENTER) {
}
}
/**
* @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 | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(PesquisaSimpl.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(() -> {
new PesquisaSimpl().setVisible(true);
});
}private void formWindowGainedFocus(java.awt.event.WindowEvent evt ) {
preencheTabela();
}
private void preencheTabela(){
DefaultTableModel modelo = (DefaultTableModel) tCSimpl.getModel();
int i = modelo.getRowCount();
while (i --> 0) {
modelo.removeRow(i);
}
List c = bd.pesquisa(tTitulo.getText());
for (Iterator it = c.iterator(); it.hasNext();) {
it.next();
modelo.addRow(new Object[]{ cadastro.getCdd(), cadastro.getTitulo(), cadastro.getAutor(), cadastro.getLocal(), cadastro.getEditora(), cadastro.getAno(), cadastro.getSituacao() });
}
}
// Variables declaration - do not modify
private javax.swing.JButton bSair;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField tAutor;
private javax.swing.JTable tCSimpl;
private javax.swing.JTextField tEditora;
private javax.swing.JTextField tTitulo;
// End of variables declaration
}
And the one in the database:
package bibl;
import java.util.List;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
/**
*
* @author admin
*/
public class BdBibl extends Bd{
public BdBibl() {
try {
conexao();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Erro: " + e.getMessage());
}
}
public void insere(BiblCadastro b){
String sql = "insert into b(registro,cdd,cutter,compl,compl2,titulo,autor,editora,local,ano,edicao,idioma,assunto,descricao,paginas,bibliografia,exemplar,formaaquis,dataaquis,fornecedor,tombo,situacao) values(?,?,?,?)";
try {
PreparedStatement ps = getCon().prepareStatement(sql);
ps.setString(1, b.getRegistro());
ps.setString(2, b.getCdd());
ps.setString(3, b.getCutter());
ps.setString(4, b.getCompl());
ps.setString(5, b.getCompl2());
ps.setString(6, b.getTitulo());
ps.setString(7, b.getAutor());
ps.setString(8, b.getEditora());
ps.setString(9, b.getLocal());
ps.setInt(10, b.getAno());
ps.setString(11, b.getEdicao());
ps.setString(12, b.getIdioma());
ps.setString(13, b.getAssunto());
ps.setString(14, b.getDescricao());
ps.setInt(15, b.getPaginas());
ps.setString(16, b.getBibliografia());
ps.setString(17, b.getExemplar());
ps.setString(18, b.getFormaaquis());
ps.setString(19, b.getDataaquis());
ps.setString(20, b.getFornecedor());
ps.setInt(21, b.getTombo());
ps.setString(22, b.getSituacao());
ps.execute();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Erro SQL: " + e.getMessage());
}
}
public void atualiza(BiblCadastro b){
String sql = "update b set registro=?,cutter=?,compl=?,compl2=?,titulo=?,autor=?,editora=?,local=?,ano=?,edicao=?,idioma=?,assunto=?,descricao=?,paginas=?,bibliografia=?,exemplar=?,formaaquis=?,dataaquis=?,fornecedor=?,tombo=?,situacao=? where cdd=?";
try {
PreparedStatement ps = getCon().prepareStatement(sql);
ps.setString(1, b.getRegistro());
ps.setString(22, b.getCdd());
ps.setString(2, b.getCutter());
ps.setString(3, b.getCompl());
ps.setString(4, b.getCompl2());
ps.setString(5, b.getTitulo());
ps.setString(6, b.getAutor());
ps.setString(7, b.getEditora());
ps.setString(8, b.getLocal());
ps.setInt(9, b.getAno());
ps.setString(10, b.getEdicao());
ps.setString(11, b.getIdioma());
ps.setString(12, b.getAssunto());
ps.setString(13, b.getDescricao());
ps.setInt(14, b.getPaginas());
ps.setString(15, b.getBibliografia());
ps.setString(16, b.getExemplar());
ps.setString(17, b.getFormaaquis());
ps.setString(18, b.getDataaquis());
ps.setString(19, b.getFornecedor());
ps.setInt(20, b.getTombo());
ps.setString(21, b.getSituacao());
ps.execute();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Erro SQL: " + e.getMessage());
}
}
public BiblCadastro localiza (String cdd){
String sql = "select * from b where cdd=?";
BiblCadastro bibl =new BiblCadastro();
try {
PreparedStatement ps = getCon().prepareStatement(sql);
ps.setString(1, cdd);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
bibl.setCdd(rs.getString("cdd"));
bibl.setRegistro(rs.getString("registro"));
bibl.setCutter(rs.getString("cutter"));
bibl.setCompl(rs.getString("compl"));
bibl.setCompl2(rs.getString("compl2"));
bibl.setTitulo(rs.getString("titulo"));
bibl.setAutor(rs.getString("autor"));
bibl.setEditora(rs.getString("editora"));
bibl.setLocal(rs.getString("local"));
bibl.setAno(rs.getInt("ano"));
bibl.setEdicao(rs.getString("edicao"));
bibl.setIdioma(rs.getString("idioma"));
bibl.setCdd(rs.getString("cdd"));
bibl.setAssunto(rs.getString("assunto"));
bibl.setDescricao(rs.getString("descricao"));
bibl.setPaginas(rs.getInt("paginas"));
bibl.setBibliografia(rs.getString("bibliografia"));
bibl.setExemplar(rs.getString("exemplar"));
bibl.setFormaaquis(rs.getString("formaaquis"));
bibl.setDataaquis(rs.getString("dataaquis"));
bibl.setFornecedor(rs.getString("fornecedor"));
bibl.setTombo(rs.getInt("tombo"));
bibl.setSituacao(rs.getString("situacao"));
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Erro SQL: " + e.getMessage());
}
return bibl;
}
/* public List pesquisaTitulo(String busca) {
String sql = "select * from b where titulo like ?";
List lista=new ArrayList();
try {
PreparedStatement ps = getCon().prepareStatement(sql);
ps.setString(1, "%"+busca+"%");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
BiblCadastro bibl = new BiblCadastro();
bibl.setCdd(rs.getString("cdd"));
bibl.setRegistro(rs.getString("registro"));
bibl.setCutter(rs.getString("cutter"));
bibl.setCompl(rs.getString("compl"));
bibl.setCompl2(rs.getString("compl2"));
bibl.setTitulo(rs.getString("titulo"));
bibl.setAutor(rs.getString("autor"));
bibl.setEditora(rs.getString("editora"));
bibl.setLocal(rs.getString("local"));
bibl.setAno(rs.getInt("ano"));
bibl.setEdicao(rs.getString("edicao"));
bibl.setIdioma(rs.getString("idioma"));
bibl.setCdd(rs.getString("cdd"));
bibl.setAssunto(rs.getString("assunto"));
bibl.setDescricao(rs.getString("descricao"));
bibl.setPaginas(rs.getInt("paginas"));
bibl.setBibliografia(rs.getString("bibliografia"));
bibl.setExemplar(rs.getString("exemplar"));
bibl.setFormaaquis(rs.getString("formaaquis"));
bibl.setDataaquis(rs.getString("dataaquis"));
bibl.setFornecedor(rs.getString("fornecedor"));
bibl.setTombo(rs.getInt("tombo"));
bibl.setSituacao(rs.getString("situacao"));
}
}
catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Erro SQL: " + e.getMessage());
}
return lista;
}
*/
public List pesquisa(String busca) {
String sql = "select * from b where cdd like ?";
List lista=new ArrayList();
try {
PreparedStatement ps = getCon().prepareStatement(sql);
ps.setString(1, "%"+busca+"%");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
BiblCadastro bibl = new BiblCadastro();
bibl.setCdd(rs.getString("cdd"));
bibl.setRegistro(rs.getString("registro"));
bibl.setCutter(rs.getString("cutter"));
bibl.setCompl(rs.getString("compl"));
bibl.setCompl2(rs.getString("compl2"));
bibl.setTitulo(rs.getString("titulo"));
bibl.setAutor(rs.getString("autor"));
bibl.setEditora(rs.getString("editora"));
bibl.setLocal(rs.getString("local"));
bibl.setAno(rs.getInt("ano"));
bibl.setEdicao(rs.getString("edicao"));
bibl.setIdioma(rs.getString("idioma"));
bibl.setCdd(rs.getString("cdd"));
bibl.setAssunto(rs.getString("assunto"));
bibl.setDescricao(rs.getString("descricao"));
bibl.setPaginas(rs.getInt("paginas"));
bibl.setBibliografia(rs.getString("bibliografia"));
bibl.setExemplar(rs.getString("exemplar"));
bibl.setFormaaquis(rs.getString("formaaquis"));
bibl.setDataaquis(rs.getString("dataaquis"));
bibl.setFornecedor(rs.getString("fornecedor"));
bibl.setTombo(rs.getInt("tombo"));
bibl.setSituacao(rs.getString("situacao"));
lista.add(bibl);
}
}
catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Erro SQL: " + e.getMessage());
}
return lista;
}
/* public List pesquisaEditora(String busca) {
String sql = "select * from b where editora like ?";
List lista=new ArrayList();
try {
PreparedStatement ps = getCon().prepareStatement(sql);
ps.setString(1, "%"+busca+"%");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
BiblCadastro bibl = new BiblCadastro();
bibl.setCdd(rs.getString("cdd"));
bibl.setRegistro(rs.getString("registro"));
bibl.setCutter(rs.getString("cutter"));
bibl.setCompl(rs.getString("compl"));
bibl.setCompl2(rs.getString("compl2"));
bibl.setTitulo(rs.getString("titulo"));
bibl.setAutor(rs.getString("autor"));
bibl.setEditora(rs.getString("editora"));
bibl.setLocal(rs.getString("local"));
bibl.setAno(rs.getInt("ano"));
bibl.setEdicao(rs.getString("edicao"));
bibl.setIdioma(rs.getString("idioma"));
bibl.setCdd(rs.getString("cdd"));
bibl.setAssunto(rs.getString("assunto"));
bibl.setDescricao(rs.getString("descricao"));
bibl.setPaginas(rs.getInt("paginas"));
bibl.setBibliografia(rs.getString("bibliografia"));
bibl.setExemplar(rs.getString("exemplar"));
bibl.setFormaaquis(rs.getString("formaaquis"));
bibl.setDataaquis(rs.getString("dataaquis"));
bibl.setFornecedor(rs.getString("fornecedor"));
bibl.setTombo(rs.getInt("tombo"));
bibl.setSituacao(rs.getString("situacao"));
}
}
catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Erro SQL: " + e.getMessage());
}
return lista;
}
*/
public void exclui (String cdd){
String sql = "delete from b where cdd=?";
try {
PreparedStatement ps = getCon().prepareStatement(sql);
ps.setString(1, cdd);
ps.execute();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Erro SQL: " + e.getMessage());
}
}
}
Pq in your fill methodTable while is with while (i --> 0) ?
– Raul Sena Ferreira
to remove the line
– Kauan Kubaski
There’s no better way to remove everything from
model
? (maybemodelo.setRowCount(0)
) Your loop is removing all the lines one by one, it seems to me. By the way,i-- > 0
is more readable if you keep it that way.– Bacco