Bom, não sei por qual motivo, mas o código não esta funcionando normal! Ele executa mas esta dando os seguintes erros:
This text will be hidden
java.lang.NullPointerException`
at visao.Estoque.PreencherTabela(Estoque.java:75)
at visao.Estoque.(Estoque.java:34)
at visao.Estoque$1.run(Estoque.java:107)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(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)
Esse é meu código:
package visao;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import controle.ConectaBanco;
import modelo.ModeloTabela;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
public class Estoque extends JFrame {
ConectaBanco conecta = new ConectaBanco();
private JPanel contentPane;
private JTable testoque;
/**
* Create the frame.
*/
public Estoque() {
conecta.conexao();
PreencherTabela("select * from produtos order by id_prod");
setIconImage(Toolkit.getDefaultToolkit().getImage(PesquisarGrupo.class.getResource("/Imagens/svce.png")));
setTitle("Estoque");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(30, 100, 1300, 600);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
panel.setPreferredSize(new Dimension(10, 300));
contentPane.add(panel, BorderLayout.NORTH);
panel.setLayout(null);
testoque = new JTable();
testoque.setBounds(5, 34, 1000, 200);
panel.add(testoque);
}
@SuppressWarnings({ "rawtypes", "unchecked", "static-access" })
public void PreencherTabela(String SQL){
ArrayList dados = new ArrayList();
String[] Colunas = new String[]{"ID"};//"Descrição","Quantidade","Data de Cadastro","Grupo","Fornecedor","Preço Custo","Preço Venda"};
conecta.executaSQL(SQL);
try {
conecta.rs.first();
do{
dados.add(new Object[]{conecta.rs.getInt("id_prod")});// conecta.rs.getString("desc_prod"), conecta.rs.getInt("qtde_prod"), conecta.rs.getString("datacad_prod"), conecta.rs.getString("grupo_prod"), conecta.rs.getString("fornecedor_prod"), conecta.rs.getDouble("precocusto_prod"), conecta.rs.getDouble("precovenda_prod")});
}while(conecta.rs.next());
} catch (SQLException e) {
JOptionPane.showMessageDialog(rootPane,"Erro ao preencher o ArrayList! \nErro: " + e);
}
ModeloTabela modelo = new ModeloTabela(dados, Colunas);
testoque.setModel(modelo);
testoque.getColumnModel().getColumn(0).setPreferredWidth(80);
testoque.getColumnModel().getColumn(0).setResizable(false);
/*testoque.getColumnModel().getColumn(1).setPreferredWidth(80);
testoque.getColumnModel().getColumn(1).setResizable(false);
testoque.getColumnModel().getColumn(2).setPreferredWidth(80);
testoque.getColumnModel().getColumn(2).setResizable(false);
testoque.getColumnModel().getColumn(3).setPreferredWidth(80);
testoque.getColumnModel().getColumn(3).setResizable(false);
testoque.getColumnModel().getColumn(4).setPreferredWidth(80);
testoque.getColumnModel().getColumn(4).setResizable(false);
testoque.getColumnModel().getColumn(5).setPreferredWidth(80);
testoque.getColumnModel().getColumn(5).setResizable(false);
testoque.getColumnModel().getColumn(6).setPreferredWidth(80);
testoque.getColumnModel().getColumn(6).setResizable(false);
testoque.getColumnModel().getColumn(7).setPreferredWidth(80);
testoque.getColumnModel().getColumn(7).setResizable(false);
testoque.getColumnModel().getColumn(8).setPreferredWidth(80);
testoque.getColumnModel().getColumn(8).setResizable(false);*/
testoque.getTableHeader().setReorderingAllowed(false);
testoque.setAutoResizeMode(testoque.AUTO_RESIZE_OFF);
testoque.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Estoque frame = new Estoque();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}