java - How to do test on Item selected In JCombobox -


i want refomuler subject clearly, because not clear.

so have 2 jcombobox. if choice item in first second display items.

the first , second jcombobox fill request mysql,

i create 2 methode,

one fill first jcombobox :

code :

public void filljcboxprj(  )   {       connexion c = new connexion();       statement s ;        resultset rs ;         try {            s = c.createstatement();      rs =c.selection("select distinct(idprojet),idpro,nomprojet projet 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();         }   } 

the sconde methode : fill seconde jcombobox dependent on selected item in first jcombobox

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

and rf n = (rf) jcombobox1.getselecteditem(); call class rf return 'num' of selected item in first jcombobox used in request,

rf **n** = (rf) jcombobox1.getselecteditem(); ..... ....   rs =c.selection("select idactiv,nomactiviter,phase activiter activiter.idprojet=**'"+n.num+"'** "); 

class rf :

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

and call methodes when application start, ,

private void formwindowopened(java.awt.event.windowevent evt) {

filljcboxprj(); filljcboxactivite();

}

but probleme is, if dont have item in first jcombobx ( no data in database table) give error in line

i guess error come 'n.num'

java.lang.nullpointerexception     @ userframe.filljcboxactivite(userframe.java:202) 

so want test on n.num nothing if first jcombobox dont have items

thanks , hope clear cause not in anglish

a couple of comments regarding code. should mark fields private , access them trough getter/setter.

class rf { private final string nom; private final string ref; private final string num; 

i don't know why final (i don't hink should) anyway. then

 rf n = (rf) jcombobox1.getselecteditem();  

for sure throws classcastexception, line never reached

 if(!(n.num.equals(""))) // dont work !! 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -