Olah, turma.
Mais uma ajuda.
O que estah errado na coneccao que tento fazer no programa abaixo (linha 662) ??? Dah o erro: java.lang.NullPointerException.
Codigo:
public class JanelaItens extends JDialog {
private String CodCarga;
public JanelaItens(Frame owner, String title, boolean modal, String CodCarga){
super(owner, title, modal);
this.CodCarga = CodCarga;
initComponents2();
try{
initCheckDBBoxes1();
}
catch(SQLException e)
{e.printStackTrace();
}
}
private void initCheckDBBoxes1() throws SQLException {
Connection conO=null;
Conecta c = new Conecta();
conO = c.getConectO();
Statement stmO = conO.createStatement();
try {
ResultSet rs = stmO.executeQuery("Select descfabrica from FABRICA");
while (rs.next()) {
jcbFabrica.addItem(rs.getString("descfabrica"));
}
rs.close();
}
catch (SQLException exception) {
exception.printStackTrace();
}
try {
ResultSet rs = stmO.executeQuery("Select codproduto from PEDPRODUTOS");
while (rs.next()) {
jcbProduto.addItem(rs.getString("codproduto"));
}
rs.close();
}
catch (SQLException exception) {
exception.printStackTrace();
}
}
private void initComponents2() {
jSeparator4 = new JSeparator();
jtfCodCarga = new JTextField();
jcbPedido = new JComboBox();
jlCodCarga = new JLabel();
jlPedido = new JLabel();
jSeparator5 = new JSeparator();
jcbFabrica = new JComboBox();
jlFabrica = new JLabel();
jlProduto = new JLabel();
jtfDescProduto = new JTextField();
jlDescProduto = new JLabel();
jtfSeq = new JTextField();
jlSeq = new JLabel();
jtfQuantde = new JTextField();
jlQuantde = new JLabel();
jcbProduto = new JComboBox();
jtfQtTot = new JTextField();
jtfQtEmb = new JTextField();
jtfQtProg = new JTextField();
jlQtTot = new JLabel();
jlQtEmb = new JLabel();
jlQtProg = new JLabel();
jSeparator6 = new JSeparator();
jbCadItem = new JButton();
jbAltItem = new JButton();
jbExcItem = new JButton();
jbSairItem = new JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jtItensCarga = new javax.swing.JTable();
getContentPane().setLayout(null);
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().add(jSeparator4);
jSeparator4.setBounds(10, 10, 680, 170);
jtfCodCarga.setText(CodCarga);
jtfCodCarga.setEditable(false);
getContentPane().add(jtfCodCarga);
jtfCodCarga.setBounds(90, 30, 80, 21);
jtfCodCarga.setHorizontalAlignment(javax.swing.JTextField.CENTER);
getContentPane().add(jcbPedido);
jcbPedido.setBounds(570, 30, 100, 21);
jlCodCarga.setFont(new java.awt.Font("MS Sans Serif", 1, 14));
jlCodCarga.setText("Carga :");
getContentPane().add(jlCodCarga);
jlCodCarga.setBounds(30, 30, 50, 19);
jlPedido.setFont(new java.awt.Font("MS Sans Serif", 1, 14));
jlPedido.setText("Pedido :");
getContentPane().add(jlPedido);
jlPedido.setBounds(500, 30, 60, 19);
getContentPane().add(jSeparator5);
jSeparator5.setBounds(10, 60, 680, 10);
getContentPane().add(jcbFabrica);
jcbFabrica.setBounds(250, 30, 240, 21);
jcbFabrica.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
String nomeFabrica = (String) jcbFabrica.getSelectedItem();
try {
Connection conO=null;
Conecta c = new Conecta();
conO = c.getConectO();
662 Statement stmO = conO.createStatement();
ResultSet rs = stmO.executeQuery(“Select codfabrica from FABRICA “+
“where descfabrica = '”+ nomeFabrica +”’”);
if (rs.next()) {
int codFabrica = rs.getInt(“codfabrica”);
System.out.println("CodFabrica: "+ codFabrica);
}
rs.close();
stmO.close();
}
catch (SQLException exception) {
exception.printStackTrace();
}
}
});
jlFabrica.setFont(new java.awt.Font("MS Sans Serif", 1, 14));
jlFabrica.setText("Fabrica :");
getContentPane().add(jlFabrica);
jlFabrica.setBounds(180, 30, 70, 19);
jlProduto.setFont(new java.awt.Font("MS Sans Serif", 1, 11));
jlProduto.setText("Produto");
getContentPane().add(jlProduto);
jlProduto.setBounds(20, 70, 60, 15);
jtfDescProduto.setEditable(false);
getContentPane().add(jtfDescProduto);
jtfDescProduto.setBounds(170, 90, 250, 21);
jlDescProduto.setFont(new java.awt.Font("MS Sans Serif", 2, 11));
jlDescProduto.setText("Descricao");
getContentPane().add(jlDescProduto);
jlDescProduto.setBounds(360, 70, 60, 15);
getContentPane().add(jtfSeq);
jtfSeq.setBounds(430, 90, 30, 21);
jlSeq.setFont(new java.awt.Font("MS Sans Serif", 1, 11));
jlSeq.setText("Seq");
getContentPane().add(jlSeq);
jlSeq.setBounds(430, 70, 30, 15);
getContentPane().add(jtfQuantde);
jtfQuantde.setBounds(630, 90, 50, 21);
jlQuantde.setFont(new java.awt.Font("MS Sans Serif", 1, 11));
jlQuantde.setText("Quantde");
getContentPane().add(jlQuantde);
jlQuantde.setBounds(630, 70, 50, 15);
getContentPane().add(jcbProduto);
jcbProduto.setBounds(20, 90, 150, 21);
jtfQtTot.setEditable(false);
jtfQtTot.setFocusable(false);
getContentPane().add(jtfQtTot);
jtfQtTot.setBounds(470, 90, 50, 21);
jtfQtEmb.setEditable(false);
jtfQtEmb.setFocusable(false);
getContentPane().add(jtfQtEmb);
jtfQtEmb.setBounds(520, 90, 50, 21);
jtfQtProg.setEditable(false);
jtfQtProg.setFocusable(false);
getContentPane().add(jtfQtProg);
jtfQtProg.setBounds(570, 90, 50, 21);
jlQtTot.setText("Qt.Tot");
getContentPane().add(jlQtTot);
jlQtTot.setBounds(470, 70, 34, 15);
jlQtEmb.setText("Qt.Emb");
getContentPane().add(jlQtEmb);
jlQtEmb.setBounds(520, 70, 34, 15);
jlQtProg.setText("Qt.Prog");
getContentPane().add(jlQtProg);
jlQtProg.setBounds(570, 70, 40, 15);
jSeparator6.setBorder(new javax.swing.border.TitledBorder("Itens Cadastrados"));
getContentPane().add(jSeparator6);
jSeparator6.setBounds(10, 180, 680, 290);
jbCadItem.setFont(new java.awt.Font("MS Sans Serif", 1, 11));
jbCadItem.setText("Cadastrar");
jbCadItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbCadItemActionPerformed(evt);
}
});
getContentPane().add(jbCadItem);
jbCadItem.setBounds(150, 130, 100, 25);
jbAltItem.setFont(new java.awt.Font("MS Sans Serif", 1, 11));
jbAltItem.setText("Alterar");
getContentPane().add(jbAltItem);
jbAltItem.setBounds(250, 130, 100, 25);
jbExcItem.setFont(new java.awt.Font("MS Sans Serif", 1, 11));
jbExcItem.setText("Excluir");
getContentPane().add(jbExcItem);
jbExcItem.setBounds(350, 130, 100, 25);
jbSairItem.setFont(new java.awt.Font("MS Sans Serif", 1, 11));
jbSairItem.setText("Sair");
jbSairItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
JanelaItens.setVisible(false);
jdCadCargasItens = new jdCadCargasItens(null,true);
}
}
);
getContentPane().add(jbSairItem);
jbSairItem.setBounds(460, 130, 100, 25);
jtItensCarga.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(jtItensCarga);
getContentPane().add(jScrollPane1);
jScrollPane1.setBounds(20, 200, 660, 260);
pack();
}
public void popularTabela() {
Vector dados = ClassRots.getItensCarga();
Vector colunas = new Vector();
colunas.add("Produto");
colunas.add("Descricao");
colunas.add("Seq");
colunas.add("Q.Tot");
colunas.add("Q.Emb");
colunas.add("Q.Prg");
colunas.add("QTDE");
jtItensCarga = new JTable(dados, colunas);
jScrollPane1.setViewportView(jtItensCarga);
}
private void jbCadItemActionPerformed(java.awt.event.ActionEvent evt) {
if (!jtfSeq.getText().equals("") && !jtfQuantde.getText().equals("") ) {
// String CodProduto = jtfCodProdut.getText();
// System.out.println("Estou no GetNome-N!"+ a + "\n" );
// String b = jcbCodNavio.getText();
String Seq = jtfSeq.getText();
String Quantde = jtfQuantde.getText();
System.out.println("Estou Antes de InsertCadCartgas");
ClassRots.insertCadItensCarga(Seq,Quantde);
System.out.println("Estou Depois de InsertCadItensCartgas");
}
else
JOptionPane.showMessageDialog(null,"\nCampos Invalidos\n","Aviso",
JOptionPane.WARNING_MESSAGE);
jtfSeq.requestFocus();
}
private JSeparator jSeparator4;
private JSeparator jSeparator5;
private JSeparator jSeparator6;
private JButton jbAltItem;
private JButton jbCadItem;
private JButton jbExcItem;
private JButton jbSairItem;
private JComboBox jcbFabrica;
private JComboBox jcbPedido;
private JComboBox jcbProduto;
private JLabel jlCodCarga;
private JLabel jlDescProduto;
private JLabel jlFabrica;
private JLabel jlPedido;
private JLabel jlProduto;
private JLabel jlQtEmb;
private JLabel jlQtProg;
private JLabel jlQtTot;
private JLabel jlQuantde;
private JLabel jlSeq;
private JTextField jtfCodCarga;
private JTextField jtfDescProduto;
private JTextField jtfQtEmb;
private JTextField jtfQtProg;
private JTextField jtfQtTot;
private JTextField jtfQuantde;
private JTextField jtfSeq;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jtItensCarga;
}
A Class Conecta contem.
…
public Connection getConectO(){
conO = connMgrO.getConnection(“pool02”, 2000);
return conO;
}
…
Este metodo estah correto. Funciona ateh chegar na linha 662 acima.
Quando executo, acontece o erro abaixo:
CodFabrica: 0
Exception occurred during event dispatching:
java.lang.NullPointerException
at com.navebrazil.exportacao.TelasNave.jdCadCargasItens$5.itemStateChanged(jdCadCargasItens.java:662)
Novamente, obrigadooooooo.
Ateh.
Formiga