0
Today my team and I came across a problem trying to do something seemingly simple: display a clerk’s surname in a Jcombobox. The problem is that when opening the screen we receive a Nullpointerexception and the Combo is empty. That’s 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 does a database query that returns the data recorded in the table tbl_work, and then inserts it into the Combobox Combonomeusuario, then I call this method in the class constructor and Actionlistener from a button. Also follows the stack of errors:
Feb 23, 2018 4:24:31 PM view.Teladeregistration GRAVE: null java.lang.Exception: Error when fetching data for User Registration! java.lang.Nullpointerexception at view.TelaDedastro.Addfunconariocombo(Teladecadastro.java:1633) at view.TelaDeCadastro.(Teladecadastro.java:104) at view.Rear$7.actionPerformed(Rear.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 of this combobox and insert - them 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 for your attention.
The error is in the file
Retaguarda.java
on line 399, stacktrace is very clear. Only with the given piece of code can’t help.– Max Fratane
What is the value of
con
when the execution enters thetry
?– igventurelli
I know it’s kind of boring to close your question as a duplicate, but not surprisingly, you weren’t the first to make that kind of mistake. Your object
con
should be null, exactly as in the case of the question I marked in the duplicate. Read the my answer in that question that should help you.– Victor Stafusa
Thank you very much! That’s right! Missing Start connection! But now how do I insert this combo data in the users table?
– Spirik