Estou com dificuldade de carregar meu Jcombobox com os dados da minha tabela do banco de dados na hora que compila esta dando vários erros
public class SubstanciaDao {
List<Substancia>substancia;
private Connection conexao;
public SubstanciaDao() throws SQLException {
this.conexao = CriaConexao.getConexao();
}
public List<Substancia> getLista() throws SQLException{
String sql = "select * from tb_substancias where nome like ? ORDER BY nome ASC";
PreparedStatement stmt = this.conexao.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
List <Substancia> listaClientes = new ArrayList<Substancia>();
while (rs.next()){
Substancia c1 = new Substancia();
c1.setFi_id(Integer.valueOf(rs.getString("fi_pk_id")));
c1.setSubstancia(rs.getString("fi_substancia"));
listaClientes.add(c1);
}
rs.close();
stmt.close();
return listaClientes;
}
}
public class Combo extends JFrame {
ArrayList <Substancia> listaSubstancia ;
private JPanel contentPane;
JComboBox cbSubstancia;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Combo frame = new Combo();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Combo() throws SQLException {
carregaCombo(); // Carregar jcombobox ao inicializar
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(null);
cbSubstancia = new JComboBox();
cbSubstancia.setBounds(96, 43, 228, 23);
panel.add(cbSubstancia);
}
public void carregaCombo()
{
DefaultComboBoxModel substancia = new DefaultComboBoxModel();
for (int i=0;i< this.listaSubstancia.size();i++)
{
substancia.addElement(this.listaSubstancia.get(i).getSubstancia());
}
cbSubstancia.setModel(substancia);
}
}
:?: :?:
Erros
java.lang.NullPointerException
at Combo.carregaCombo(Combo.java:64)
at Combo.<init>(Combo.java:44)
at Combo$1.run(Combo.java:29)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:602)
at java.awt.EventQueue$1.run(EventQueue.java:600)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:611)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) :?: :?: