2016-10-01 1 views
-1

J'essaie de faire une fonction pour définir le JCombobox quand un élément de modèle est sélectionné, il affiche un ID non modifiable dans le JTextField ci-dessous. mais il y a une erreur dans cette ligne = pstm.setString (1, pridetalhe.getPrioridade()); suit Voici le code:PreparedStatement throws NullPointerException dans SetString

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

Son appel dans le formulaire:

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

Et voici la trace de pile:

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)

Je pense bizarre déjà que je déclarais tous les objets et les initialisés, ce qui est faux sur elle ?! Merci beaucoup!!

Répondre

0

Le problème est que vous avez votre pridetalhe est déclaré null dans le code mentionné ci-dessous:

PrioridadeDetalhe pridetalhe = null; 

Et puis il est null pendant que vous exécutez la ligne ci-dessous mentionné:

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

Et donc jeter NullPointer exception, pour éviter l'exception initier avant toute opération sur votre pridetalhe objet

+0

J'ai résolu le problème maintenant, j'ai déclaré en haut de la fonction PrioridadeDetalhe pridetalhe = new PrioridadeDetalhe(); Débogué la fonction entière, maintenant le problème est seulement à la ligne (rs.next()) que ce n'est pas l'exécution qui est à l'intérieur de ce bloc, j'essaie de découvrir pourquoi ... merci pour l'aide mhasan !! –

+0

Raison est que vous n'obtenez pas de résultats de dB à partir de votre requête exécuter votre requête et voir si elle renvoie des résultats – mhasan

+0

Hi mhasan !! Désolé pour le retard, Un grand merci pour l'aide !! ça m'a aidé et j'ai corrigé le problème + ce résultat Résoudre le problème :) J'ai voté ici et accepté ta réponse –