0

I am trying to insert from textfields data into my database. Insert data to a table compte_utilisateur and I've 2 comboboxes : first for groupe containing names of all groups (alias libelle_groupe) in my database, and second for statut containing all status values (alias valeur) in my database.

I want to insert into my db a user ( alias compte_utilisateur) and to do that i need id_groupe of the name of group selected in the combobox and id_statut of the status selected in my combobox.

I've tried this, but i'm having an exception : java.lang.NullPointerException

the code :

ResultSet valeur1= stmtListeLivre.executeQuery("select id_groupe from groupe where libelle_groupe='"+combo_name.getSelectedItem().toString()+"'");

ResultSet valeur2= stmtListeLivre1.executeQuery("select id_statut from statut where valeur='"+jComboBox1.getSelectedItem().toString()+"'");

Number val1 =  ((Number) valeur1.getObject(1)).intValue();

Number val2 =  ((Number) valeur2.getObject(1)).intValue();

 String requete="INSERT INTO compte_utilisateur(Id_compte, Nom, Prenom, Matricule, Id_groupe, Id_statut) VALUES (?,?,?,?,?,?)";


    pst=maConnexion.ObtenirConnexion().prepareStatement(requete);
    pst.setString(1, jTIdf.getText());
    pst.setString(2, jTNom.getText());
    pst.setString(3, jTPrenom.getText());
    pst.setString(4, jTMatricule.getText());  
    pst.setString(5, val1.toString()); 
    pst.setString(6, val2.toString()); 
    pst.execute();

1 Answers1

0

There are probably any number of problems with this, but lets start with what seems obvious...

ResultSet valeur1= stmtListeLivre.executeQuery("select id_groupe from groupe where libelle_groupe='"+combo_name.getSelectedItem().toString()+"'");
ResultSet valeur2= stmtListeLivre1.executeQuery("select id_statut from statut where valeur='"+jComboBox1.getSelectedItem().toString()+"'");
Number val1 =  ((Number) valeur1.getObject(1)).intValue();
Number val2 =  ((Number) valeur2.getObject(1)).intValue();

Neither valeur1 or valeur2 have been primed to the first (possible) record.

When a ResultSet is returned, it's cursor (which record it's currently pointing to) is set to before the first record. You need to check if the ResultSet has a next record or not. Doing so will, if possible, move the cursor to the next record (or return false indicating it's reached the end of the ResultSet)

Number val1 =  null;
Number val2 =  null;
if (valeur1.next()) {
    val1 =  ((Number) valeur1.getObject(1)).intValue();
}
if (valeur2.next()) {
    val2 =  ((Number) valeur2.getObject(1)).intValue();
}
MadProgrammer
  • 323,026
  • 21
  • 204
  • 329
  • I've tried this, nothing changes still have the same error – lamia bennis Aug 18 '14 at 04:59
  • If you can't provide us with either a [runnable example](https://stackoverflow.com/help/mcve) or the line at which the code fails, there is nothing anybody can do... – MadProgrammer Aug 18 '14 at 05:01
  • I'm having error in that line : ResultSet valeur2= stmtListeLivre1.executeQuery("select id_statut from statut where valeur='"+jComboBox1.getSelectedItem().toString()+"'"); – lamia bennis Aug 18 '14 at 05:03
  • Then `stmtListeLivre1` is `null` or `jComboBox1` is `null` or what is been returned by `jComboBox1.getSelectedItem()` is `null`. Time to dig the debugger out and inspect those values... – MadProgrammer Aug 18 '14 at 05:05
  • I've checked all my statements and it works now. Thank u for ur help. I've just another little problem, once one of my textfields is selected, the comboboxes doesn't work, i can't select from values, only the one that appears is added. – lamia bennis Aug 18 '14 at 05:19
  • Nothing in you code example provides any hints at why that might be so... – MadProgrammer Aug 18 '14 at 05:21
  • Ok i'll check it by myself and see where is the prob, thank u very much for ur help – lamia bennis Aug 18 '14 at 05:26
  • I still have the problem, when I fill the textfields, the comboboxes stop working, it doesn't allow me choose anything. But before filling the textfields i can select from the values displayed by the comboboxes. Can u please help me resloving that problem – lamia bennis Aug 18 '14 at 16:08
  • Create a runnable demo which demonstrates your problem and post a new question, without an example, it's impossible to provide any kind of feed back – MadProgrammer Aug 18 '14 at 21:28