J'ai trouvé mon erreur :
j'avais completement oublié qu'avant de créer un Vector, j'avais fait un tableau, et comme un niais, je l'ai initialisé à 200, une taille aléatoire mais supérieure au nombre d'éléments réels, du coup, quand il veut acceder à un élément n qui n'a pas été initialisé, forcément....


Bonjour,
Je fais un petit programme en java, et je cherche à créer une liste de String, ces String étant des données membres d'une classe que j'ai crées. J'ai également fait une fonction qui crée toute une liste d'objets de ma classe, et j'ai une erreur NullPointerException quand je veux récuperer la donnée membre, quelqu'un pourrait me dire pourquoi ? Voici les extraits de fichiers sources importants :
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
 
package donnees;
 
public class CArmesCC {
    public String  NomArme;
    int     Degats;
    int     Vit;
    int     ForR1;
    int     ForR2;
    String  Mode1;
    String  Mode2;
    String  Categorie;
    String  Special;
    int     Sol;
    int     Frac;
    int     Pres;
 
    public CArmesCC (String PNomArme, int PDegats, int PVit, int PForR1, int PForR2, String PMode1, String PMode2,
                     String PCategorie,    String PSpecial, int PSol, int PFrac,int PPres) {
 
        NomArme     = new String (PNomArme);
        Degats         = new Integer (PDegats);
        Vit         = new Integer (PVit);
        ForR1         = new Integer (PForR1);
        ForR2        = new Integer (PForR2);
        Mode1        = new String (PMode1);
        Mode2         = new String (PMode2);
        Categorie     = new String (PCategorie);
        Special     = new String (PSpecial);
        Sol         = new Integer (PSol);
        Frac         = new Integer (PFrac);
        Pres         = new Integer (PPres);
    } // Constructeur
 
 
 
}
fichier de créations du vecteur de CArmesCC
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
 
package donnees;
 
import java.util.Vector;
 
public class CBanqueArmes {
 
    public Vector <CArmesCC> TabArmesCC = new Vector <CArmesCC> ();
 
 
    private void InitTabArmesCC () {
 
        TabArmesCC.setSize(200);
 
        //TabArmesCC.add(new CArmesCC(PNomArme, PDegats, PVit, PForR1, PForR2, PMode1, PMode2, PCategorie, PSpecial, PSol, PFrac, PPres));
 
    // LIVRE DE REGLES - ARMES DE BASE 
        TabArmesCC.add(new CArmesCC("Hallebarde"            , 60, -15, 6, 11, "TR","CON", "Hast / Deux mains"    , "Deux mains"                                , 15,  4, 20));
        TabArmesCC.add(new CArmesCC("Harpon"                , 35,  -5, 5, 0, "PER", "", "Hast"                , "Une ou deux mains, Lançable"                , 11,  0, 15));
        TabArmesCC.add(new CArmesCC("Chaîne"                , 25,     0, 6, 0, "CON", "", "Corde"                , "Complexe, Immobilisation(For 8)"         , 13,  2, 15));
        TabArmesCC.add(new CArmesCC("Cestus"                , 25,  10, 3, 0, "PER", "TR", "Arme Courte"            , ""                                        , 11, -2, 15));
 
// Ca continue longtemps comme ça
fichier où j'ai l'erreur

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
package Interface_Graphique;
 
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
 
import javax.swing.*;
 
 
import donnees.CBanqueArmes;
import donnees.CArmesCC;
 
@SuppressWarnings("serial")
public class CInventaire extends JPanel {
 
    CInventaire  () {
        // ...
 
    } // constructeur
 
    private void NewArme () {
        final JFrame NewObj = new JFrame ("Armes");
 
        JPanel Objets = new JPanel();
        Objets.setLayout(new BoxLayout (Objets, BoxLayout.Y_AXIS));
        Objets.setBorder(BorderFactory.createEmptyBorder(10, 5, 2, 5    ));
 
        // -- CONTENU FENETRE NEW ARME -- //
        CBanqueArmes TabArmes = new CBanqueArmes();    
        Vector <String> TabNomArme = new Vector <String> ();
 
        for (int i = 0; i < TabArmes.TabArmesCC.size(); ++i)
            TabNomArme.add(TabArmes.TabArmesCC.elementAt(i).NomArme);
 
        JList ListeArme = new JList (TabNomArme);
        ListeArme.setLayoutOrientation(JList.VERTICAL);
        ListeArme.setDragEnabled(false);
        ListeArme.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        ListeArme.setSize(300, 200);
 
        JScrollPane PanneauListe = new JScrollPane(ListeArme, ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
 
        JButton OK         = new JButton("Ok");
        JButton Annuler = new JButton("Annuler");
 
        Annuler.addActionListener(new ActionListener() {
 
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                NewObj.dispose();
            }
        });
 
        JPanel Bouton = new JPanel();
        Bouton. setLayout(new FlowLayout());
        Bouton. add(OK);
        Bouton. add(Annuler);
 
        Objets.add(PanneauListe);
        Objets.add(Bouton);
        NewObj.add(Objets);
        NewObj.setPreferredSize(new Dimension(200, 100));
        NewObj.setResizable(false);
        NewObj.pack();
        NewObj.setVisible(true);
 
    } // NewArme ()
les messages d'erreurs renvoyés sont, avec à la ligne 382, le contenu de la boucle for, en 270 le début de la fonction NewArme () et en 57 l'appel de la fonction NewArme () dans le constructeur de la classe CInventaire :

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
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at Interface_Graphique.CInventaire.NewArme(CInventaire.java:282) 
    at Interface_Graphique.CInventaire.access$0(CInventaire.java:270) 
    at Interface_Graphique.CInventaire$1.actionPerformed(CInventaire.java:57) 
    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.dispatchEventImpl(Unknown Source)   
  at java.awt.EventQueue.access$000(Unknown Source) 
    at java.awt.EventQueue$1.run(Unknown Source)  
   at java.awt.EventQueue$1.run(Unknown Source)  
   at java.security.AccessController.doPrivileged(Native Method)   
  at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)   
  at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)   
  at java.awt.EventQueue$2.run(Unknown Source)   
  at java.awt.EventQueue$2.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)     
at java.awt.EventQueue.dispatchEvent(Unknown Source)  
   at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)   
  at java.awt.EventDispatchThread.pumpEventsForFilter(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)
Merci d'Avance, Bonne Soirée