Exception in thread "main" java.lang.NullPoin

Fermé
romses1 Messages postés 17 Date d'inscription jeudi 12 mai 2011 Statut Membre Dernière intervention 12 mai 2015 - 26 mai 2011 à 11:11
poukkid Messages postés 106 Date d'inscription mercredi 20 mai 2009 Statut Membre Dernière intervention 11 avril 2012 - 26 mai 2011 à 11:49
Bonjour,


j'ai un projet en java qui me casse la tête m'affiche un code donc je ne comprend pas pourtant avant il fonctionnai a merveille .voici le code
-------------------------------------------------pour la connection----------------------------------


//package hopital;
import java.sql.*;

import java.sql.PreparedStatement.*;

public class ConnectDB
{

static Connection con=null;
static ResultSet resultats = null;

static Message msg = new Message();
public ConnectDB(String DSN)
{

//**chargement du pilote
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException e)
{
msg.error("impossible de charger le pilote jdbc:odbc");

}

try
{
String DBurl="jdbc:odbc:"+DSN;
con= DriverManager.getConnection(DBurl);
}
catch(SQLException c)
{
msg.error("imposible de se connecter a la BD "+c.getMessage());
System.exit(0);
}

}
//********************************************************
public void SQLupdate(String req)
{ try
{
//PreparedStatement stmt = con.prepareStatement(req);
Statement stmt=con.createStatement();
stmt.executeUpdate(req);
msg.dialog("requette executée avec succes");
}
catch (SQLException e)
{
msg.error("Impossible d'executer la requete: \n"+e.getMessage());
}
}

//----------------------------


public void SQLquery(String sql)
{

try {
Statement stmt=con.createStatement();
stmt.executeQuery(sql);
}
catch (SQLException e) {
msg.error("Impossible d'exécuter la requête"+e.getMessage() );
System.exit(0);
}

}
//********************************

public int MaxID(String table,String ID) {
int nbre = 0;
SQLquery("SELECT MAX("+ID+") AS nbre FROM "+table);
try {
ResultSetMetaData rsmd = resultats.getMetaData();
int nbCols = rsmd.getColumnCount();

boolean encore = resultats.next();
while (encore)
{
nbre = resultats.getInt("nbre");
encore = resultats.next();

}
resultats.close();
}
catch (SQLException e)
{
msg.error(e.getMessage());
}

return nbre;
}
//*********************

public int numRows(String sql)
{
int nbre = 0;
SQLquery(sql);
try {
ResultSetMetaData rsmd = resultats.getMetaData();
int nbCols = rsmd.getColumnCount();
boolean encore = resultats.next();
while (encore)
{
encore = resultats.next();
nbre++;
}
resultats.close();
}
catch (SQLException e)
{
msg.error(e.getMessage());
}
return nbre;
}

//*****************************************************

public int numCols(String sql)
{
int nbre = 0;
SQLquery(sql);
try {
ResultSetMetaData rsmd = resultats.getMetaData();
nbre = rsmd.getColumnCount();
resultats.close();
}
catch (SQLException e)
{
msg.error(e.getMessage());
}
return nbre;
}

//************************************

public String[][] DataQuery(String query) {
int rows = numRows(query);
int cols = numCols(query);
String[][] donnees = new String[rows][cols];

SQLquery(query);
try {
ResultSetMetaData rsmd = resultats.getMetaData();
int nbCols = rsmd.getColumnCount();
boolean encore = resultats.next();
int numTuple = 0;
while (encore)
{
for(int i = 0; i < nbCols; i++) {
donnees[numTuple][i] = resultats.getString(i+1);
}
encore = resultats.next();
numTuple++;
}
resultats.close();



}
catch (SQLException e)
{
msg.error(e.getMessage());
}

return donnees;

}


//*******************************
public static void main(String args[])
{

}
}

-----------------------------------pour l'affichage des données de ma bd----------------------------
/*
* @(#)Affichage.java
*
*
* @author
*
*/
import java.sql.*;
import javax.swing.JComponent;
import javax.swing.*;
import java.awt.ScrollPane;

public class Affichage extends JFrame {

public String [][] docinfo;
public JLabel enreg = new JLabel ("Liste des enregistrements") ;
public JPanel panne=new JPanel();
public ConnectDB con;
ResultSet résultats = null;



public Affichage() {

this.setSize(600,500);
enreg.setSize(600,250);
con = new ConnectDB("Projet.accdb");
String requete = "select * from Descriptiftablesql where infolibre='oui'";
docinfo = con.DataQuery(requete);
//Statement st= createStatement();
//ResultSet res = st.executeQuery(requete);



String html = "" ;
html += "<html><body bgcolor = 'green'><table border='1'><tr><td colspan = '6' align ='CENTER'><font color = 'red' >LISTE DES ENREGISTREMENTS<font size='5'> LISTE D</font></font></td></tr>" ;
html += "<tr><td>DESCRIPTIFSQL</td><td>CHAMPS_SQL</td><td>TYPE_DE_FICHIER</td><td>VALIDATION</td><td>QUANTITE</td><td>prix</td></tr>" ;
for(int i = 0; i < docinfo.length; i++){

html += "<tr><td>"+docinfo[i][0]+"</td><td>"+docinfo[i][1]+"</td><td>"+docinfo[i][2]+"</td><td>"+docinfo[i][3]+"</td><td>"+docinfo[i][4]+"</td><td>"+docinfo[i][5]+"</td></tr>" ;
}

html += "</table></body></html>" ;
enreg.add(new JScrollPane(new JLabel(html, JLabel.CENTER)));
panne.add(new JLabel(html, JLabel.CENTER));

this.add(panne);
enreg.setVisible(true);

}
public static void main(String[] args) {
new Affichage().setVisible(true);
}


}
----------------------------------------------------------------------------------------compilation---------------
losrque je compile voici lerreur ke joptient



Exception in thread "main" java.lang.NullPointerException
at ConnectDB.numRows(ConnectDB.java:104)
at ConnectDB.DataQuery(ConnectDB.java:142)
at Affichage.<init>(Affichage.java:29)
at Affichage.main(Affichage.java:52)

Process completed.
sil vous plait aidez moi


A voir également:

1 réponse

poukkid Messages postés 106 Date d'inscription mercredi 20 mai 2009 Statut Membre Dernière intervention 11 avril 2012 11
26 mai 2011 à 11:49
public int numRows(String sql)
	{
		int nbre = 0;
		SQLquery(sql);
		try {
			ResultSetMetaData rsmd = resultats.getMetaData();


Je pense que ton erreur vient de cet attribut "resultat" qui au moment ou tu essaye de faire .getMetaData() dessus est toujours null. il n'est pas remplit avant...
Tu ne le remplit pas dans le constructeur ni autre part avant d'essayer d'appeler des méthodes dessus, et comme tu l'as initialisé à null, tu obtiens cette erreur...
La ligne 104 de ton code correspond-t-elle à la ligne :
ResultSetMetaData rsmd = resultats.getMetaData();

?
Si oui, alors le problème doit bien venir de là, sinon peux tu ecrire la ligne correspondante?
0