PreparedStatement在SetString

时间:2016-10-01 23:40:45

标签: java database jdbc nullpointerexception

我正在尝试在选择任何模型项时设置JCombobox的功能,它在下面的JTextField中显示不可编辑的ID。 但是这行中的错误= pstm.setString(1,pridetalhe.getPrioridade()); 以下是代码:

@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;
    }

他在表格中的电话:

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);
       }
    }  

这是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)

我认为我已经宣布了所有对象并初始化它们,它有什么不对,我觉得很奇怪?!非常感谢!!

1 个答案:

答案 0 :(得分:0)

问题是您在下面提到的代码中声明了pridetalhe null

PrioridadeDetalhe pridetalhe = null;

当你执行下面提到的行时,它是null

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

因此抛出NullPointer异常,以避免异常在pridetalhe对象上的任何操作之前启动它