2016-10-01 63 views
-1

我想要做一個函數來設置JCombobox,當任何模型項被選中時,它會在下面的JTextField中顯示一個不可編輯的ID。 但在這行中出現錯誤= pstm.setString(1,pridetalhe.getPrioridade()); 這裏下面的代碼:PreparedStatement在SetString中拋出NullPointerException

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

這裏是堆棧跟蹤:

run: 
 
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
 
\t at br.com.jdbc.victor.dao.entidadesdao.MySQLPrioridadeDetalheDAO.getCodPrioridade(MySQLPrioridadeDetalheDAO.java:113) 
 
\t at br.com.jdbc.victor.view.FormNovaChamada.cbPrioridadeDetalheActionPerformed(FormNovaChamada.java:285) 
 
\t at br.com.jdbc.victor.view.FormNovaChamada.access$000(FormNovaChamada.java:26) 
 
\t at br.com.jdbc.victor.view.FormNovaChamada$1.actionPerformed(FormNovaChamada.java:109) 
 
\t at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1258) 
 
\t at javax.swing.JComboBox.contentsChanged(JComboBox.java:1332) 
 
\t at javax.swing.JComboBox.intervalRemoved(JComboBox.java:1352) 
 
\t at javax.swing.AbstractListModel.fireIntervalRemoved(AbstractListModel.java:179) 
 
\t at javax.swing.DefaultComboBoxModel.removeAllElements(DefaultComboBoxModel.java:174) 
 
\t at javax.swing.JComboBox.removeAllItems(JComboBox.java:771) 
 
\t at br.com.jdbc.victor.view.FormNovaChamada.<init>(FormNovaChamada.java:58) 
 
\t at br.com.jdbc.victor.view.FormNovaChamada.lambda$main$1(FormNovaChamada.java:482) 
 
\t at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311) 
 
\t at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756) 
 
\t at java.awt.EventQueue.access$500(EventQueue.java:97) 
 
\t at java.awt.EventQueue$3.run(EventQueue.java:709) 
 
\t at java.awt.EventQueue$3.run(EventQueue.java:703) 
 
\t at java.security.AccessController.doPrivileged(Native Method) 
 
\t at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) 
 
\t at java.awt.EventQueue.dispatchEvent(EventQueue.java:726) 
 
\t at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) 
 
\t at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) 
 
\t at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) 
 
\t at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) 
 
\t at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) 
 
\t at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

我想很奇怪已經是我聲明的所有對象,並初始化它們,什麼是錯的嗎?!非常感謝!!

回答

0

的問題是,你有你的pridetalhe在下文提到的代碼中聲明null

PrioridadeDetalhe pridetalhe = null; 

,然後將其null,而你正在執行下面提到行:

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

,因此拋NullPointer異常,以避免異常在對您的對象pridetalhe對象進行任何操作之前啓動該異常

+0

我現在已經解決了這個問題,我已經在函數的頂部聲明瞭PrioridadeDetalhe pridetalhe = new PrioridadeDetalhe();調試了整個函數,現在問題只出現在while(rs.next())行,它沒有在這個塊內執行,我試圖發現爲什麼......感謝幫助mhasan! –

+0

原因是你沒有從你的查詢得到dB的結果執行你的查詢,看看它是否返回結果 – mhasan

+0

嗨mhasan !!對不起,延遲,非常感謝您的幫助!它幫助我,我解決了問題+ resultSet問題:)我在這裏投票並接受了你的答案 –

相關問題