-1

I'm trying to do a function to set the JCombobox when any model item is selected it displays a non editable ID in the JTextField below. but it's getting error in this line = pstm.setString(1, pridetalhe.getPrioridade()); Here follows the code:

@Override
    public Long getCodPrioridade() throws DAOException{
        PrioridadeDetalhe pridetalhe = null;
        String GETCOD = null;
        long valor = 0;
        
        try {
            GETCOD = "SELECT * from prioridadedetalhe where prioridade = ? ";
            pstm = con.prepareStatement(GETCOD);
            // \/ ERROR IN THIS LINE!
            pstm.setString(1, pridetalhe.getPrioridade());
            rs = pstm.executeQuery();
            while(rs.next()){
                pridetalhe = new PrioridadeDetalhe();
                pridetalhe.setIdPrioridadeDetalhe(rs.getLong("idPrioridadeDetalhe"));
            }
        } catch (SQLException ex) {
            throw new DAOException("Erro no SQL", ex);
        } finally {
            if(pstm!=null){
                try {
                    pstm.close();
                } catch (SQLException ex) {
                    throw new DAOException("Erro ao fechar conexão", ex);
                }
            } if(rs!=null){
                try {
                    rs.close();    
                } catch (SQLException ex) {
                    throw new DAOException("Erro ao fechar conexão", ex);
                }           
            }
        }        
        return valor;
    }

His call in the Form:

private void cbPrioridadeDetalheActionPerformed(java.awt.event.ActionEvent evt) {                                                    
       try{
          MySQLDaoManager man = new MySQLDaoManager("root", "", "localhost", "atendimentos", 3306);  
          Long input = man.getPrioridadeDetalheDAO().getCodPrioridade();
          tfIdPrioridadeDetalhe.setText(String.valueOf(input));
       }  catch (SQLException ex){
           
       }  catch (DAOException ex) {           Logger.getLogger(FormNovaChamada.class.getName()).log(Level.SEVERE, null, ex);
       }
    }  

And here is the Stack Trace:

run:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
 at br.com.jdbc.victor.dao.entidadesdao.MySQLPrioridadeDetalheDAO.getCodPrioridade(MySQLPrioridadeDetalheDAO.java:113)
 at br.com.jdbc.victor.view.FormNovaChamada.cbPrioridadeDetalheActionPerformed(FormNovaChamada.java:285)
 at br.com.jdbc.victor.view.FormNovaChamada.access$000(FormNovaChamada.java:26)
 at br.com.jdbc.victor.view.FormNovaChamada$1.actionPerformed(FormNovaChamada.java:109)
 at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1258)
 at javax.swing.JComboBox.contentsChanged(JComboBox.java:1332)
 at javax.swing.JComboBox.intervalRemoved(JComboBox.java:1352)
 at javax.swing.AbstractListModel.fireIntervalRemoved(AbstractListModel.java:179)
 at javax.swing.DefaultComboBoxModel.removeAllElements(DefaultComboBoxModel.java:174)
 at javax.swing.JComboBox.removeAllItems(JComboBox.java:771)
 at br.com.jdbc.victor.view.FormNovaChamada.<init>(FormNovaChamada.java:58)
 at br.com.jdbc.victor.view.FormNovaChamada.lambda$main$1(FormNovaChamada.java:482)
 at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
 at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
 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:76)
 at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
 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)

I'm thinking it weird already that i declared all the objects and initialized them, what is wrong on it?! Thanks a lot!!

theEarlyRiser
  • 134
  • 12

1 Answers1

0

The problem is that you have your pridetalhe is declared null in below mentioned code:

PrioridadeDetalhe pridetalhe = null;

And then it is null while you are executing below mentioned line:

line = pstm.setString(1, pridetalhe.getPrioridade());

And hence throwing NullPointer exception,to avoid the exception initiate it before any operation on your pridetalhe object

mhasan
  • 3,703
  • 1
  • 18
  • 37
  • I've solved the problem now, i've declared at the top of the function PrioridadeDetalhe pridetalhe = new PrioridadeDetalhe(); debugged the entire function, now the problem is only at the while(rs.next()) line that it isn't executing that is inside this block, i'm trying to discover why... thanks for the help mhasan!! – theEarlyRiser Oct 02 '16 at 02:31
  • Reason is you are not getting results from dB from your query execute your query and see if it is returning results – mhasan Oct 02 '16 at 02:38
  • Hi mhasan!! Sorry for the delay, Big thanks for the help!! it helped me and i fixed the problem + that resultSet problem :) I voted here and accepted your answer – theEarlyRiser Oct 03 '16 at 15:43