Exception in thread "main" java.lang.NullPointerException

Résolu/Fermé
guns65 Messages postés 32 Date d'inscription mardi 28 septembre 2010 Statut Membre Dernière intervention 26 février 2014 - 26 déc. 2012 à 23:55
guns65 Messages postés 32 Date d'inscription mardi 28 septembre 2010 Statut Membre Dernière intervention 26 février 2014 - 27 déc. 2012 à 15:32
salut tou le monde !! mon probleme c'est loreque j'execute mon programme j'ai ca

Exception in thread "main" java.lang.NullPointerException
at Application.fermeture(Application.java:436)
at Application.main(Application.java:448)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Fournisseur.actionPerformed(Application.java:114)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

Process completed.


voici mon code !!

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
/////////////////////////////////////////onglet fournisseur//////////////////////////////////////////////////////////////
class Fournisseur extends JPanel implements ActionListener {
public Connection con = null;
public Statement st= null;
public ResultSet rs= null;



JLabel l1 = new JLabel(" Id-F");
JLabel l2 = new JLabel(" Nom-F");
JLabel l3 = new JLabel(" Prenom-F");
JLabel l4 = new JLabel(" Adresse");
JLabel l5 = new JLabel(" N° Telephone");

JLabel l6 = new JLabel("");
JLabel l7 = new JLabel("");
JLabel l8 = new JLabel("");

JTextField t1 = new JTextField();
JTextField t2 = new JTextField();
JTextField t3 = new JTextField();
JTextField t4 = new JTextField();
JTextField t5 = new JTextField();


JButton bt1 = new JButton("Ajouter");
JButton bt2 = new JButton("Supprimer");

public JPanel p1 = new JPanel();


public Fournisseur() {




p1.setLayout(new GridLayout(5, 3, 3,3));
p1.add(l1);
p1.add(t1);
p1.add(bt1);
p1.add(l2);
p1.add(t2);
p1.add(bt2);
p1.add(l3);
p1.add(t3);
p1.add(l6);
p1.add(l4);
p1.add(t4);
p1.add(l7);
p1.add(l5);
p1.add(t5);
p1.add(l8);

bt1.addActionListener(this);
bt2.addActionListener(this);

this.add(p1);
this.setSize(400,400);

}

/////////////// recuperer le text dans le textfield ///////////////////////////////////////////////////////////////////

public String gett1() {

return t1.getText();
}
//////
public String gett2() {

return t2.getText();
}
//////
public String gett3() {

return t3.getText();
}
//////
public String gett4() {

return t4.getText();
}
//////
public String gett5() {

return t5.getText();
}
//////////////////////////////executer le requete lors de lappuis du bouton //////////////////////////////////////////

public void actionPerformed(ActionEvent e){

Object src=e.getSource();
if(src instanceof JButton){
JButton var=(JButton) src;
///////////////////////ajouter//////////////////////
if (var==bt1) {
try
{
st=con.createStatement();
String req ="INSERT INTO Fournisseur VALUES('"+gett1()+"','"+gett2()+"','"+gett3()+"','"+gett4()+"','"+gett5()+"')";
int c =st.executeUpdate(req);
}
catch(SQLException e1){e1.printStackTrace(); }
}
////////////////////supprimer//////////////
else if (var==bt2){
try
{
st=con.createStatement();
String req2="DELETE * FROM Fournisseur WHERE nom =('"+gett2()+"') ";
int b =st.executeUpdate(req2);
}
catch(SQLException e2){e2.printStackTrace(); }

}
}}

}
//////////////////////////////////////////////////////onglet Client//////////////////////////////////////////////////


class Client extends JPanel implements ActionListener {
public Connection con = null;
public Statement st= null;
public ResultSet rs= null;



JLabel l1 = new JLabel(" Id-C");
JLabel l2 = new JLabel(" Nom-C");
JLabel l3 = new JLabel(" Prenom-C");
JLabel l4 = new JLabel(" Adresse");
JLabel l5 = new JLabel(" N° Telephone");

JLabel l6 = new JLabel("");
JLabel l7 = new JLabel("");
JLabel l8 = new JLabel("");

JTextField t1 = new JTextField();

JTextField t2 = new JTextField();
JTextField t3 = new JTextField();
JTextField t4 = new JTextField();
JTextField t5 = new JTextField();


JButton bt1 = new JButton("Ajouter");
JButton bt2 = new JButton("Supprimer");

public JPanel p2 = new JPanel();


public Client() {




p2.setLayout(new GridLayout(5, 3, 3,3));
p2.add(l1);
p2.add(t1);
p2.add(bt1);
p2.add(l2);
p2.add(t2);
p2.add(bt2);
p2.add(l3);
p2.add(t3);
p2.add(l6);
p2.add(l4);
p2.add(t4);
p2.add(l7);
p2.add(l5);
p2.add(t5);
p2.add(l8);

bt1.addActionListener(this);
bt2.addActionListener(this);

this.add(p2);
this.setSize(400,400);

}

/////////////// recuperer le text dans le textfield ///////////////////////////////////////////////////////////////////

public String gett1() {

return t1.getText();
}
//////
public String gett2() {

return t2.getText();
}
//////
public String gett3() {

return t3.getText();
}
//////
public String gett4() {

return t4.getText();
}
//////
public String gett5() {

return t5.getText();
}
//////////////////////////////executer le requete lors de lappuis du bouton //////////////////////////////////////////

public void actionPerformed(ActionEvent e){

Object src=e.getSource();
if(src instanceof JButton){
JButton var=(JButton) src;
///////////////////////ajouter//////////////////////
if (var==bt1) {
try
{
st=con.createStatement();
String req ="INSERT INTO Fournisseur VALUES('"+gett1()+"','"+gett2()+"','"+gett3()+"','"+gett4()+"','"+gett5()+"')";
int c =st.executeUpdate(req);
}
catch(SQLException e1){e1.printStackTrace(); }
}
////////////////////supprimer//////////////
else if (var==bt2){
try
{
st=con.createStatement();
String req2="DELETE * FROM Table1 WHERE nom =('"+gett2()+"') ";
int b =st.executeUpdate(req2);
}
catch(SQLException e2){e2.printStackTrace(); }

}
}}

}
////////////////////////////////////// onglet Produit ///////////////////////////////////////////////////////////////////////////

class Produit extends JPanel implements ActionListener {

public Connection con = null;
public Statement st= null;
public ResultSet rs= null;

//////////////////////////////////////////////////////////


JLabel l1 = new JLabel(" Nom-P");
JLabel l2 = new JLabel(" N° Serie");
JLabel l3 = new JLabel(" Prix");

JTextField t1 = new JTextField();
JTextField t2 = new JTextField();
JTextField t3 = new JTextField();

JButton bt1 = new JButton("Ajouter");
JButton bt2 = new JButton("Supprimer");

public JPanel p3 = new JPanel();

public Produit() {


p3.setLayout(new GridLayout(3, 3, 3,3));
p3.add(l1);
p3.add(t1);
p3.add(bt1);
p3.add(l2);
p3.add(t2);
p3.add(bt2);
p3.add(l3);
p3.add(t3);

bt1.addActionListener(this);
bt2.addActionListener(this);

this.add(p3);
this.setSize(400,400);

}
/////////////// recuperer le text dans le textfield ///////////////////////////////////////////////////////////////////

public String gett1() {

return t1.getText();
}
//////
public String gett2() {

return t2.getText();
}
//////
public String gett3() {

return t3.getText();
}
//////

public void actionPerformed(ActionEvent e){

Object src=e.getSource();
if(src instanceof JButton){
JButton var=(JButton) src;
///////////////////////ajouter//////////////////////
if (var==bt1) {
try
{
st=con.createStatement();
String req ="INSERT INTO Fournisseur VALUES('"+gett1()+"','"+gett2()+"','"+gett3()+"')";
int c =st.executeUpdate(req);
}
catch(SQLException e1){e1.printStackTrace(); }
}
////////////////////supprimer//////////////
else if (var==bt2){
try
{
st=con.createStatement();
String req2="DELETE * FROM Table1 WHERE Nom-P =('"+gett1()+"') ";
int b =st.executeUpdate(req2);
}
catch(SQLException e2){e2.printStackTrace(); }

}
}}
}
//////////////////////////////////////////Application principale ///////////////////////////////////////////////////////////////////

public class Application extends JFrame implements ActionListener {
public Connection con = null;
public Statement st= null;
public ResultSet rs= null;

public JMenuBar menu= new JMenuBar();
JTabbedPane onglet = new JTabbedPane();

public Application(){


super("gestion de stock");
setSize(400,400);
setVisible(true);
setDefaultCloseOperation(this.EXIT_ON_CLOSE);

Container content = this.getContentPane();
onglet.addTab("Fournisseur",new Fournisseur());
onglet.addTab("Client",new Client());
onglet.addTab("Produit",new Produit());
content.add(BorderLayout.CENTER ,onglet);
addmenu();
this.setJMenuBar(menu);
this.show();

}

/////////////////////////////////////////////// menu ////////////////////////////////////////////////////////////////////////////

public void addmenu() {



JMenu Fichier = new JMenu("Fichier");
JMenu Help = new JMenu("Help");
JMenu Basculer = new JMenu("Basculer");
JMenuItem Quitter = new JMenuItem("Quiter");
JMenuItem Fournisseur = new JMenuItem("Fournisseur");
JMenuItem Client = new JMenuItem("Client");
JMenuItem Produit = new JMenuItem("Produit");

menu.add(Fichier);
menu.add(Help);
Fichier.add(Quitter);
Fichier.add(Basculer);

Basculer.add(Fournisseur);
Basculer.add(Client);
Basculer.add(Produit);

Quitter.addActionListener(this);
Fournisseur.addActionListener(this);
Client.addActionListener(this);
Produit.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand();
if(cmd.equals("Fournisseur"))
onglet.setSelectedIndex(0);
if(cmd.equals("Client"))
onglet.setSelectedIndex(1);
if(cmd.equals("Produit"))
onglet.setSelectedIndex(2);
if(cmd.equals("Quitter"))
{//JOptionPane.showMessageDialog("ffffffffffffff");
//int a= Integer.parseInt(JOptionPane.showInputDialog("ok"));
System.exit(0);
//this.dispose();

}
}


////////////////////////////////////se connecter a la base //////////////////////////////////////////////////////////////////
public void connection(){


try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e5){
System.out.println("impossible de chargern les drivers");
}
try{
String url="jdbc:odbc:test";
con=DriverManager.getConnection(url, "","");
}
catch(SQLException e5)
{
System.out.println("impossible de créer une connexion");
}
}


public void fermeture(){

try {
//rs.close();
st.close();
con.close();
}
catch(SQLException e){}
}


public static void main(String[] args) {

Application interface1 = new Application();

interface1.connection();
interface1.fermeture();
interface1.pack();
interface1.show();



}


}


merci pour votre aide :)

5 réponses

KX Messages postés 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 3 015
27 déc. 2012 à 00:31
Exception in thread "main" java.lang.NullPointerException
at Application.fermeture(Application.java:436)

Le message d'erreur indique une erreur ligne 436 :
st.close();
Or "st" n'est affecté qu'une seule fois, ligne 339 :
public Statement st= null;
Tu ne peux pas faire null.close(), d'où le NullPointerException...
D'ailleurs quel intérêt de fermer "st" puisqu'il n'existe pas ?
1