Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to do test on Item selected In JCombobox
    text
    copied!<p>I want refomuler my subject clearly, because it was not clear.</p> <p>so i have two JCombobox. if i choice a item in the first the second display the items.</p> <p>the first and the second JCombobox are fill with request from mysql, </p> <p>i create two methode, </p> <p>One to fill the first JCombobox : </p> <p>Code :</p> <pre><code>public void fillJCBOXPrj( ) { connexion c = new connexion(); Statement s ; ResultSet rs ; try { s = c.createStatement(); rs =c.selection("SELECT Distinct(IdProjet),idpro,NomProjet FROM projet where projet.iduser='"+this.getid()+"' "); while(rs.next()) { String num = rs.getString("idpro"); String nom = rs.getString("NomProjet"); String ref = rs.getString("IdProjet"); jComboBox1.addItem(new RF(nom,ref,num)); } } catch (Exception ex) { ex.printStackTrace(); } } </code></pre> <p>the sconde methode : fill the seconde JCombobox dependent on the Selected Item in first JCombobox</p> <pre><code>Code : public void fillJCBOXActivite() { RF n = (RF) jComboBox1.getSelectedItem(); connexion c = new connexion(); Statement s ; ResultSet rs ; try { s = c.createStatement(); System.out.println(n.num); rs =c.selection("SELECT idactiv,NomActiviter,Phase FROM activiter WHERE activiter.IDProjet='"+n.num+"' "); while(rs.next()) { String num = rs.getString("idactiv"); String nom = rs.getString("NomActiviter"); String ref = rs.getString("Phase"); jComboBox3.addItem(new RF(nom,ref,num)); } } catch (Exception ex) { ex.printStackTrace(); } } </code></pre> <p>and <code>RF n = (RF) jComboBox1.getSelectedItem();</code> it call class RF to return the 'num' of selected item in the first JCombobox Which used in request,</p> <pre><code>RF **n** = (RF) jComboBox1.getSelectedItem(); ..... .... rs =c.selection("SELECT idactiv,NomActiviter,Phase FROM activiter WHERE activiter.IDProjet=**'"+n.num+"'** "); </code></pre> <p>Class RF :</p> <pre><code>class RF { public final String nom; public final String ref; public final String num; public RF(String nom, String ref, String num) { this.nom = nom; this.num = num; this.ref = ref; } @Override public String toString() { return ref +" - " +nom ; } } </code></pre> <p>and finally i do call the methodes when application start, so i do this ,</p> <p>private void formWindowOpened(java.awt.event.WindowEvent evt) { </p> <p>fillJCBOXPrj(); fillJCBOXActivite();</p> <p>} </p> <p>But the probleme is, if i dont have any Item in the first JCombobx ( no data in DataBase Table) then it give a error in this line </p> <p>i guess the error come from 'n.num' </p> <pre><code>java.lang.NullPointerException at UserFrame.fillJCBOXActivite(UserFrame.java:202) </code></pre> <p>so want to do test on n.num that to do nothing if the first JCombobox dont have Items </p> <p>Thanks for help and i hope is clear now cause am not good in Anglish </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload