Tuesday, May 22, 2012

Making multiple query to mysql in one java event

I'm having and error when trying to make multiple queries in one event. I have a table where I have a list of all the films with the main actor, producer, genre etc.



The actor, producers and genres are stocked as an int which in another table refers to a String. I'm therefore making multiple queries to print out all the data in a textarea.
However i keep getting the error



Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at tp2.MainPage.jList1ValueChanged(MainPage.java:231)
at tp2.MainPage.access$300(MainPage.java:24)
at tp2.MainPage$4.valueChanged(MainPage.java:106)


I refered to all the lines mentioned, line 24 and 106 are generated by netbeans so i doubt the error comes from there. Line 231 is mentioned in the code below.



    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {
String npays = "null",titre= "null",nacteurPrincipal= "null",oscar= "null",realisateur= "null";
try {
String url = "jdbc:mysql://localhost/mycinema";
Class.forName("com.mysql.jdbc.Driver");
Connection connexion = DriverManager.getConnection(url, "root", "");
Statement inst = connexion.createStatement();
titre = jList1.getSelectedValue().toString(); //LINE 231
ResultSet resultat = inst.executeQuery("SELECT * FROM film WHERE titre = '" + titre + "'");
while (resultat.next()) {
npays = resultat.getString("npays");
realisateur = resultat.getString("realisateur");
nacteurPrincipal = resultat.getString("nacteurPrincipal");
oscar = resultat.getString("oscar");
}
resultat = inst.executeQuery("SELECT * FROM pays WHERE npays =" + npays);
while (resultat.next()) {
npays = resultat.getString("nom");
}

resultat = inst.executeQuery("SELECT * FROM acteur WHERE nacteur =" + nacteurPrincipal);
while (resultat.next()) {
nacteurPrincipal = resultat.getString("prenom") + resultat.getString("nom");
}

jTextArea3.setText(titre + " : un film par " + realisateur + "\n" + npays);
} catch (ClassNotFoundException ex) {
Logger.getLogger(MainPage.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(MainPage.class.getName()).log(Level.SEVERE, null, ex);
}
}


If somebody can spot my error pleas point in out thankyou :)



P.S the variables are in french. srry for that :/





No comments:

Post a Comment