Today my team and I encountered a problem when trying to do something seemingly simple: display the surname of an official in a JComboBox. The problem is that at the time of opening the screen we receive a NullPointerException and the Combo is empty. This is how we did it:
private void AddFunconarioCombo () throws Exception
{
PreparedStatement ps = null;
ResultSet rs = null;
try{
String sql = "select nome_funcionario, sobrenome_funcionario from tbl_funcionario where flag_ativo =1";
ps = this.con.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next())
{
ComboNomeUsuario.removeAllItems();
ComboNomeUsuario.addItem(rs.getString("nome_funcionario") + " " + rs.getString("sobrenome_funcionario"));
}
} catch (Exception e) {
throw new Exception("Erro ao buscar os dados para o cadastro de Usuários! " + e.toString());
}
finally
{
ConnectionFactory.CloseConnection(con, ps, rs);
}
}
The above method makes a database query that returns the data registered in the tbl_funcionario table, and then inserts into the ComboBox ComboNameUsuario, then I call this method in the class constructor and in the Actionlistener of a button. Here's the error stack:
Feb 23, 2018 4:24:31 PM view.CatalogueTelevision SERIOUS: null java.lang.Exception: Error fetching the data for the user registry! java.lang.NullPointerException at view.CadastroTree.AddFunconarioCombo (Cadastro.java:1633) at view.Catalogue Screen. at view.Relay $ 7.actionPerformed (Rearguard.java:399) at javax.swing.AbstractButton.fireActionPerformed (AbstractButton.java:2022) at javax.swing.AbstractButton $ Handler.actionPerformed (AbstractButton.java:2348) at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed (DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased (BasicButtonListener.java:252) at java.awt.Component.processMouseEvent (Component.java:6533) at javax.swing.JComponent.processMouseEvent (JComponent.java:3324) at java.awt.Component.processEvent (Component.java:6298) at java.awt.Container.processEvent (Container.java:2236) at java.awt.Component.dispatchEventImpl (Component.java:4889) at java.awt.Container.dispatchEventImpl (Container.java:2294) at java.awt.Component.dispatchEvent (Component.java:4711) at java.awt.LightweightDispatcher.retargetMouseEvent (Container.java:4888) at java.awt.LightweightDispatcher.processMouseEvent (Container.java:4525) at java.awt.LightweightDispatcher.dispatchEvent (Container.java:4466) at java.awt.Container.dispatchEventImpl (Container.java:2280) at java.awt.Window.dispatchEventImpl (Window.java:2746) at java.awt.Component.dispatchEvent (Component.java:4711) at java.awt.EventQueue.dispatchEventImpl (EventQueue.java:758) at java.awt.EventQueue.access $ 500 (EventQueue.java:97) at java.awt.EventQueue $ 3.run (EventQueue.java:709) at java.awt.EventQueue $ 3.run (EventQueue.java:703) at java.security.AccessController.doPrivileged (Native Method) at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege (ProtectionDomain.java:80) at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege (ProtectionDomain.java:90) at java.awt.EventQueue $ 4.run (EventQueue.java:731) at java.awt.EventQueue $ 4.run (EventQueue.java:729) at java.security.AccessController.doPrivileged (Native Method) at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege (ProtectionDomain.java:80) at java.awt.EventQueue.dispatchEvent (EventQueue.java:728) 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)
And after that, how do I use the data from this combobox and insert it into the database? Follow the DAO Code
public void Salvar (Usuario u) throws Exception{
PreparedStatement ps = null;
if (u == null)
{
throw new Exception("Erro: Usuario não pode ser nulo!");
}
try {
String sql = "insert into tbl_usuario (id_usuario, login_usuario, senha_usuario, fk_funcionario, flag_ativo)"
+ "values (NEXTVAL('sequencia_usuario'),?,?,CURRVAL('sequencia_funcionario'),1)";
ps = this.con.prepareStatement(sql);
ps.setString(1, u.getLoginUsuario());
ps.setString(2, u.getSenhaUsuario());
ps.executeUpdate();
ps.close();
} catch (Exception e) {
throw new Exception("Erro ao inserir os dados!" + e.getMessage());
}
finally{
ConnectionFactory.CloseConnection(con, ps);
}
}
Thanks in advance for your attention.