IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Java Discussion :

erreur java.lang.NullPointerException impossible à résoudre


Sujet :

Langage Java

  1. #1
    Membre confirmé Avatar de oceane751
    Profil pro
    Intégrateur Web
    Inscrit en
    Novembre 2004
    Messages
    1 280
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Intégrateur Web

    Informations forums :
    Inscription : Novembre 2004
    Messages : 1 280
    Points : 575
    Points
    575
    Par défaut erreur java.lang.NullPointerException impossible à résoudre
    bonjour

    je pense que la question que je vais vous poser, vous l'avez dejà vu plein de fois..

    je commence à peter un boulon lol car impossible de résoudre mon soucis, meme apres des recherches sur google et sur ce site.

    mon erreur : echec pilote : java.lang.NullPointerException

    (j'utilise JDBC pour connexion à Mysql)

    j'ai lu pour ce genre d'erreur : "appel d'une methode ou utilisation d'un champ avec réference null" mais dans mon code je ne vois pas où il y a un null....


    voici le code qui me permet d'effectuer l'addition des prix correspondant au nom des elements cochés
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
     
     
     
     
    import javax.swing.*;
     
    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
    import java.util.Enumeration;
    import java.util.Hashtable;
    import java.util.Properties;
    import java.math.BigDecimal;
     
     
     
     
    public class carte extends JFrame implements ActionListener
    //, ItemListener
    //implements ItemListener 
    {
     
    //	Properties : Cette classe est un dictionnaire permettant d'associer deux chaînes de caractères. 
        private Properties props = new Properties();
    //	 Hashtable() : permet de stocker des valeurs en les associant à des 
        // identificateurs uniques calculés à partir de clés
        private Hashtable associationProduitsPrix = new Hashtable();
     
    	private JPanel panel;//Déclaration de l'objet JPanel	
    	private JLabel prixx, type;
        private JButton bouton;
        private Checkbox sale,chek;
     
     
        public carte()
    	{
     
     
    		panel = new JPanel();
    		panel.setLayout(new FlowLayout(FlowLayout.LEFT,5,2));
     
     
    		String pilote = "com.mysql.jdbc.Driver";
    		try
    		{
    			//Chargement de mon pilote
    			Class.forName(pilote);
    			//Connexion à ma base mysql avec mon login et mot de passe 
    			Connection connexion = DriverManager.getConnection("jdbc:mysql://localhost/creperie","root","root");
    			//Création de mon statement qui va me permettre d'executer mes requetes
    			Statement instruction = connexion.createStatement();
     
    			ResultSet resultatt = instruction.executeQuery("SELECT DISTINCT type FROM carte ");
    			while(resultatt.next())
    			{
    				type = new JLabel(resultatt.getString("type"));
    				panel.add(type);
     
     
    			Statement instructionn = connexion.createStatement();	
    			ResultSet resultat = instructionn.executeQuery("SELECT nom, prix FROM carte WHERE type = '"+resultatt.getString("type")+"'");
     
     
    			while(resultat.next())
    			{
     
    				String nom = props.getProperty(resultat.getString("nom"));
     
    				sale = new Checkbox(nom);
     
    				String prix = props.getProperty(resultat.getString("prix"));
    				prixx = new JLabel(prix);
     
    				Produit produit = new Produit();
    				produit.setPrix(new BigDecimal(prix));
     
    				//sale.addItemListener(this);
    				//panel.add(type);
    				panel.add(sale);
    				associationProduitsPrix.put(sale,produit);
     
     
    			} 
    			}
     
     
     
    		}
    		catch (Exception e)
    		{
     
    			System.out.println("echec pilote : "+e);
    		}
     
    		bouton = new JButton("ticket");
    		panel.add(bouton);
    		bouton.addActionListener(this);//On ajoute la fenêtre en tant qu'écouteur du bouton*/
     
     
     
    	}
     
     
     
     
    /*Checkbox che;
     
    	public void itemStateChanged(ItemEvent evt)
    	{
    		che= (Checkbox)evt.getSource();
    	}*/
     
     
    	public void actionPerformed(ActionEvent e) 
        {
    		if(e.getSource() == bouton)
    		{
    			BigDecimal total = new BigDecimal("0"); //initialise à 0
                 Enumeration enu = associationProduitsPrix.keys();
     
     
    			while (enu.hasMoreElements())
    			{
    				chek = (Checkbox)enu.nextElement();
     
    				if (chek.getState())
    				{
     
    					Produit produit = (Produit)associationProduitsPrix.get(chek);
    					BigDecimal prix = produit.getPrix();
     
    					total = total.add(prix);
    				}
    			}
     
                  JOptionPane.showMessageDialog(null, "total :" +total);
     
     
    		}
        }
     
    }
    voilà...

    merci ebaucoup de vouloir m'aider.

    en attendant je vais continuer à chercher..

  2. #2
    Membre actif Avatar de g0up1l
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    341
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 341
    Points : 294
    Points
    294
    Par défaut
    Voila par exemple une ligne d'où peut venir l'exception.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Statement instruction = connexion.createStatement();
    si ta connexion a échouée, connexion = null

  3. #3
    Membre actif Avatar de g0up1l
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    341
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 341
    Points : 294
    Points
    294
    Par défaut
    un conseil, peaufine un peu ton code pour cibler mieux tes exceptions !

    exemple :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
     try{// la connexion n'existait pas donc on la cree
     
    	    	co=DriverManager.getConnection(getDbur(), getUser(), getPass());
     
     
    	    }
    	    catch(SQLException sqle) {
    	    	System.err.println(sqle.getMessage()) ;
    		throw new MappingException("connexion impossible ");
    	    }

  4. #4
    Membre confirmé Avatar de oceane751
    Profil pro
    Intégrateur Web
    Inscrit en
    Novembre 2004
    Messages
    1 280
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Intégrateur Web

    Informations forums :
    Inscription : Novembre 2004
    Messages : 1 280
    Points : 575
    Points
    575
    Par défaut
    ah....

    donc le pb pourrait venir du fait que j'ai 2 requetes.
    mais j'ai lu qu'il fallait creer 2 fois (dans mon cas) le statment en changeant juste le nom du statment car resultset ferme la connexion mysql...

  5. #5
    Membre actif Avatar de g0up1l
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    341
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 341
    Points : 294
    Points
    294
    Par défaut
    Et si tu mettais une trace après chaque ligne suspecte ? tu saurais où ça plante.

  6. #6
    Membre confirmé Avatar de oceane751
    Profil pro
    Intégrateur Web
    Inscrit en
    Novembre 2004
    Messages
    1 280
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Intégrateur Web

    Informations forums :
    Inscription : Novembre 2004
    Messages : 1 280
    Points : 575
    Points
    575
    Par défaut
    avec ton bout de code, cela me souligne en rouge à ce niveau là :
    Class.forName(pilote);

  7. #7
    Membre confirmé Avatar de oceane751
    Profil pro
    Intégrateur Web
    Inscrit en
    Novembre 2004
    Messages
    1 280
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Intégrateur Web

    Informations forums :
    Inscription : Novembre 2004
    Messages : 1 280
    Points : 575
    Points
    575
    Par défaut
    J'ai mis ça :
    catch(Exception e) {
    System.err.println(e.getMessage()) ;
    }

    et ya marqué comme erreur : "null"

    de plus, cette ligne "throw new MappingException("connexion impossible ");" , le MappingException est souligné en rouge..

  8. #8
    Membre actif Avatar de g0up1l
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    341
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 341
    Points : 294
    Points
    294
    Par défaut
    Citation Envoyé par oceane751
    J'ai mis ça :
    catch(Exception e) {
    System.err.println(e.getMessage()) ;
    }




    et ya marqué comme erreur : "null"
    mais où ? t'as 'entouré' ta création de connexion, je suppose ? Dans ce cas, c'est que bel et bien ta connexion a échoué

    de plus, cette ligne "throw new MappingException("connexion impossible ");" , le MappingException est souligné en rouge..
    Ben ouais, c'est mon exemple, MappingException est une classe perso

  9. #9
    Membre confirmé Avatar de oceane751
    Profil pro
    Intégrateur Web
    Inscrit en
    Novembre 2004
    Messages
    1 280
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Intégrateur Web

    Informations forums :
    Inscription : Novembre 2004
    Messages : 1 280
    Points : 575
    Points
    575
    Par défaut
    oauis mais mon code de connexion à la base marche pour d'autre code

    regarde ce que j'ai comme erreur maintenant :

    java.lang.NullPointerException
    at java.math.BigDecimal.<init>(Unknown Source)
    at carte.<init>(carte.java:74)
    at AcceuilCrepe.actionPerformed(AcceuilCrepe.java:58)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)


    c'est une veritable catastrophe..

  10. #10
    Membre actif Avatar de g0up1l
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    341
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 341
    Points : 294
    Points
    294
    Par défaut
    T'inquiète, c'est en fait simple à analyser, regarde il faut lire en partant du bas :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    java.lang.NullPointerException
    at java.math.BigDecimal.<init>(Unknown Source)
    at carte.<init>(carte.java:74)
    at AcceuilCrepe.actionPerformed(AcceuilCrepe.java:58)
    Seules ces lignes sont importantes.
    Donc tu dois cliquer sur le bouton : AcceuilCrepe
    qui déclenche la création d'un objet 'carte'
    dans lequel tu créés un BigDecimal -> et c'est ça qui foire !

  11. #11
    Membre confirmé Avatar de oceane751
    Profil pro
    Intégrateur Web
    Inscrit en
    Novembre 2004
    Messages
    1 280
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Intégrateur Web

    Informations forums :
    Inscription : Novembre 2004
    Messages : 1 280
    Points : 575
    Points
    575
    Par défaut
    ah oki!!!

    dans ma classe AcceuilCrepe, pour acceder à la classe carte je fais ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    if ( (JButton) e.getSource() == ticket ) 
    
    			{
    carte ca = new carte(); ==>LIGNE 58
    				
    			}
    dans ma classe carte :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Produit produit = new Produit();
    produit.setPrix(new BigDecimal(prix)); ==> LIGNE 74
    et dans ma classe Produit :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    public void setPrix(BigDecimal decimal)
        {
            prix = decimal;
        }

    donc dans la classe carte, j'appelle peut etre mal le setPrix....

  12. #12
    Membre actif Avatar de g0up1l
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    341
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 341
    Points : 294
    Points
    294
    Par défaut
    Non, je pense plutôt que la variable 'prix' que tu passes au constructeur BigDecimal est pas ou mal initialisé.
    Regarde ton exception :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    java.math.BigDecimal.<init>(Unknown Source)
    c'est là que ça foire

  13. #13
    Membre confirmé Avatar de oceane751
    Profil pro
    Intégrateur Web
    Inscrit en
    Novembre 2004
    Messages
    1 280
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Intégrateur Web

    Informations forums :
    Inscription : Novembre 2004
    Messages : 1 280
    Points : 575
    Points
    575
    Par défaut
    alors dans AcceuilCrepe :
    String prix =null;

    et dans ma classe Produit :
    public BigDecimal prix=null;

    c'est ça en faite? initialiser à null?

  14. #14
    Membre actif Avatar de g0up1l
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    341
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 341
    Points : 294
    Points
    294
    Par défaut
    Ben ouais, quand tu fais :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    new BigDecimal( prix )
    il ne faut pas que prix = null ;

    Essaye dans AccueilCrepe :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    String prix = "0.0" ;

  15. #15
    Membre confirmé Avatar de oceane751
    Profil pro
    Intégrateur Web
    Inscrit en
    Novembre 2004
    Messages
    1 280
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Intégrateur Web

    Informations forums :
    Inscription : Novembre 2004
    Messages : 1 280
    Points : 575
    Points
    575
    Par défaut
    ça ne marche po

  16. #16
    Membre actif Avatar de g0up1l
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    341
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 341
    Points : 294
    Points
    294
    Par défaut
    Ah ouais, j'ai regardé la doc, c'est plus compliqué que ça quand tu veux initialiser un BigDecimal à partir d'une String

    et si tu fais :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
     
    new BigDecimal(0.0);

  17. #17
    Membre confirmé Avatar de oceane751
    Profil pro
    Intégrateur Web
    Inscrit en
    Novembre 2004
    Messages
    1 280
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Intégrateur Web

    Informations forums :
    Inscription : Novembre 2004
    Messages : 1 280
    Points : 575
    Points
    575
    Par défaut
    non plus ...;

  18. #18
    Membre actif Avatar de g0up1l
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    341
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 341
    Points : 294
    Points
    294
    Par défaut
    c'est toujours la même exception que tu as ?

  19. #19
    Membre confirmé Avatar de oceane751
    Profil pro
    Intégrateur Web
    Inscrit en
    Novembre 2004
    Messages
    1 280
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Intégrateur Web

    Informations forums :
    Inscription : Novembre 2004
    Messages : 1 280
    Points : 575
    Points
    575
    Par défaut
    en faite j'avais dejà une base qui marchait mais qui fonctionne avec un fichier texte.

    j'ai essayé de le refaire moi même en m'aidant de cet code et avec une BDD Mysql

    la classe "carte"
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
     
    package com.carte_crepee.test;
     
    import java.awt.Button;
    import java.awt.Checkbox;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Frame;
    import java.awt.Label;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.math.BigDecimal;
    import java.math.BigInteger;
    import java.util.Enumeration;
    import java.util.Hashtable;
    import javax.swing.JOptionPane;
    import javax.swing.*;
    import java.awt.Choice;
    import java.util.Properties;
    import java.io.*;
    import java.awt.*;
    import com.carte_crepee.outil.Produit;
     
    public class carte extends Frame implements ActionListener
    {
        private Properties props = new Properties();
        private Hashtable associationProduitsPrix= new Hashtable();
     
        private GridLayout grid;
             private TextField text;
             private TextField serveur;
                private Label entree;
                 private Label crepesalee;
                 private Label crepesucree;
                 private Label boisson;
                private Checkbox salade;
     
                private Label lab;
                private Label lab2;
                private Label lab3;
     
                private Choice maCombo;
                private Checkbox melon;
                private Label cs;
            private Checkbox jf;
          private Button bouton;
        private Button quitter;
     
        class WindowEventHandler extends WindowAdapter
        {
            public void windowClosing(WindowEvent we)
            {
                dispose(); // quitte l'objet
            }
        }
     
        public carte()
        {
            super("creperie");
            addWindowListener(new WindowEventHandler());
            // Lit les données du fichier de propriétés
            //Properties props = new Properties();
            try
            {
                props.load(new FileInputStream("carte.txt"));
            }
            catch (FileNotFoundException e)
            {
                System.err.println("Fichier carte.txt non trouvé");
            }
            catch (IOException e)
            {
                System.err.println("Impossible de lire le fichier carte.txt");
            }
            // Création des champs
            //nombre d'article = au nombre de clé dans le fichier properties
            //puisque chaque article forme un couple de clés libellé/prix
            entree = new Label ("entree");
                add(entree);
                crepesalee = new Label("crepe salée");
                add(crepesalee);
                crepesucree = new Label("crepe sucree");
                add(crepesucree);
                boisson = new Label("boisson");
                add(boisson);
     
                Panel panel = new Panel();
                  GridLayout jo = new GridLayout(3, 2, 5, 5);
                     panel.setLayout(jo);
                     panel.applyComponentOrientation( ComponentOrientation.RIGHT_TO_LEFT);
     
     
     
            int length=props.size()/2;
            for (int i=0;i<length;i++)
            {
     
                String valeur = props.getProperty("libelle_"+i);
                //String crepe = props.getProperty("libelle_crepe_"+i);
                if (valeur!=null)
                {
     
                    Choice combo = new Choice();
                    combo.add("1");
                    combo.add("2");
                   add(combo);
     
     
                    Checkbox checkbox = new Checkbox(valeur);
                    add(checkbox);
     
                    String prix = props.getProperty("prix_"+i);
                    Label label = new Label(prix);
                    add(label);
                    Produit produit = new Produit();
                    produit.setComboQuantite(combo);
                    produit.setPrix(new BigDecimal(prix));
     
                    // ajout dans la table de hash pour le calcul du prix
                    // la clé étant l'objet chechbox correspondant à l'article
                    associationProduitsPrix.put(checkbox, produit);
     
     
                }
            }
            // on met les boutons
             text = new TextField ("numero de table");
             add(text);
             serveur = new TextField ("nom du serveur");
             add(serveur);
            bouton = new Button("ticket");
            add(bouton);
            quitter = new Button("quitter l'application");
            add(quitter);
            // on intialise les évènements sur les boutons
            initBoutonsListeners();
            // Initialise la fenêtre
            setLayout(new FlowLayout()); //sinon n'affiche qu'un seul element ds la fenetre
            setBackground(Color.white);
            setForeground(Color.black);
     
     
     
     
     
            quitter.addActionListener(this);
        }
     
        // Initialise les évènements des boutons
        private void initBoutonsListeners()
        {
            bouton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    BigDecimal total = new BigDecimal("0.00"); //initialise à 0
                    Enumeration enu = associationProduitsPrix.keys();
                    while (enu.hasMoreElements())
                    {
                        Checkbox check = (Checkbox)enu.nextElement();
                        if (check.getState()) // si un check a été coché
                        {
                            Produit produit = (Produit)associationProduitsPrix.get(check);
                            BigDecimal prix = produit.getPrix();
                            BigDecimal quantite = new BigDecimal (new BigInteger(produit.getComboQuantite().getSelectedItem()));
                            total = total.add(prix.multiply(quantite));
     
                        }
                    }
                    JOptionPane.showMessageDialog(null,"A la bonne crêpe" + "\n" + "67 rue de vaugirard" + "\n" + "75006 paris" + "\n" + " Tel : 01.45.44.38.46" + "\n" + "serveur : " + serveur.getText() + "\n" +   "Total : " + total+ "\n" +   "Numero de table : "+text.getText()+ "\n" );
                }
            });
        }
        public void actionPerformed(ActionEvent event)
        {
            if (event.getSource() == quitter)
            {
                dispose();
                System.exit(0);
            }
        }
     
     
        public static void main(String[] args)
        {
            Frame f = new carte();
     
     
               // f.setContentPane(panel);
                     f.setTitle("Ticket client");
                   f.setBounds(100,100,300,200);
     
            f.pack();
            f.setSize(500,500);
            f.show();
        }
    }
    et la classe "produit"
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
     
    package com.carte_crepee.outil;
     
     
    import java.awt.Choice;
    import java.math.BigDecimal;
     
    public class Produit
    {
        private BigDecimal prix;
        private Choice comboQuantite;
     
        /**
         * @return
         */
        public Choice getComboQuantite()
        {
            return comboQuantite;
        }
     
        /**
         * @return
         */
        public BigDecimal getPrix()
        {
            return prix;
        }
     
        /**
         * @param choice
         */
        public void setComboQuantite(Choice choice)
        {
            comboQuantite = choice;
        }
     
        /**
         * @param decimal
         */
        public void setPrix(BigDecimal decimal)
        {
            prix = decimal;
        }
     
    }
    j'ai peut etre mal recopié quelque chose ou ché pa koa.. je verifie, je verifie..

  20. #20
    Membre confirmé Avatar de oceane751
    Profil pro
    Intégrateur Web
    Inscrit en
    Novembre 2004
    Messages
    1 280
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Intégrateur Web

    Informations forums :
    Inscription : Novembre 2004
    Messages : 1 280
    Points : 575
    Points
    575
    Par défaut
    Citation Envoyé par g0up1l
    c'est toujours la même exception que tu as ?
    vui

+ Répondre à la discussion
Cette discussion est résolue.
Page 1 sur 2 12 DernièreDernière

Discussions similaires

  1. Erreur java. lang.NullPointerException
    Par hicham.gi dans le forum Struts 1
    Réponses: 17
    Dernier message: 03/06/2009, 11h11
  2. Erreur java.lang.NullPointerException hibernate
    Par sousoujda2 dans le forum Hibernate
    Réponses: 8
    Dernier message: 11/03/2008, 19h32
  3. Réponses: 0
    Dernier message: 26/12/2007, 17h28
  4. [Débutant] Erreur java.lang.NullPointerException
    Par Kevin12 dans le forum Struts 1
    Réponses: 2
    Dernier message: 12/02/2007, 15h48
  5. Probleme erreur java.lang.NullPointerException
    Par Tsukaasa dans le forum Langage
    Réponses: 4
    Dernier message: 25/05/2006, 18h19

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo