• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

 
nadia ghezaiel
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have this exception:::
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Conn.Connexion.insertUtilisateur(Connexion.java:50)

And I don't know what's the matter!!!

My code is:


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Conn;
import java.sql.Connection;
import java.sql.*;
import javax.swing.JOptionPane;

/**
*
* @author Nadia
*/
public class Connexion {

java.sql.Connection C;

private Connection C1;
String Nom;
String Adresse;
String MDP;
String VMDP;
/**
* Constructeur
*/
public Connexion() {
//this.connection=ConnexionBDD.getInstance();

try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
C1= DriverManager.getConnection("jdbc:sqlserver://localhost\\A:1433;databaseName=Authentif","sa","sqlserver");
Statement stmt = C1.createStatement();
}

catch(Exception e){}

}

//*************Insertion Utilisateur

// public int insertUtilisateur(UserIdentit utilisateur) {


public void insertUtilisateur(String Nom,String Adresse,String MDP,String VMDP) {

PreparedStatement pst;
try {


pst = C1.prepareStatement("INSERT INTO Authentif.dbo.tab_authentif (Nomc,Adressec,MDP,VMDP)"+"VALUES(?,?,?,?)");
pst.setString(1,Nom);
pst.setString(2,Adresse);
pst.setString(3,MDP);
pst.setString(4,VMDP);
int res=pst.executeUpdate();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null,ex.getMessage());
}


/////A voir Absolument
/*pst = C.prepareStatement(requete);
pst.setString(1,utilisateur.getNom());
pst.setString(2,utilisateur.getAdresseC());
pst.setString(3, utilisateur.getMDP());
pst.setString(4, utilisateur.getVMDP());*/

/* resultat = pst.executeUpdate();
pst.close();
pst = null;
} catch (SQLException e) {
System.out.println("Erreur lors de l'insertion de l'utilisateur.");
e.printStackTrace();
}

return resultat;*/


}

/////***************************Connecter utilisateur********************************
public void seConnecter(String Nom,String MDP)
{
PreparedStatement pst;
try {


pst = C.prepareStatement("SELECT nom FROM Authentif.dbo.tab_autnentif WHERE login='" + Nom + "' AND mdp='"+MDP+"'");
pst.setString(1,Nom);
pst.setString(3,MDP);
int res=pst.executeUpdate();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null,ex.getMessage());
}




}
}

Please help me!!!je suis bloqué à ce niveau,je pense que le code n'est pas erroné:!!
 
Campbell Ritchie
Sheriff
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We only speak English on this forum. No other languages permitted. You are saying you are blocked at this stage and you think the code is not in error.

There is definitely an error; for example you have an empty catch block. That would allow an Exception to be ignored in creating your connection C1, and you will not know about it until the connection is null and you suffer an Exception elsewhere. Put something useful in every catch block (even e.printStackTrace();), so you know whether an Exception has occurred or not. Then you can work whether the real cause is your Connection being null.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic