Bonjour, je rencontre l'erreur suivante quand je lance mon programme: java.lang.ArrayIndexOutOfBoundsException
erreur qui n’apparaît pas toujours mais qui peut également apparaître 10 fois de suite et jamais au même moment
il y a beaucoup d'ArrayList et je fais beaucoup de condition avec ArrayList.size()..
Je ne trouve pas le problème, je débute en java, merci pour votre aide.
Je vous laisse les deux classes (dont la principale) qui semble en être la cause:
Code:
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 import java.applet.Applet; import java.applet.AudioClip; import java.awt.Color; import java.awt.Font; import java.io.File; import java.util.ArrayList; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class Main { static int x; //numero joueur qui joue static int z; //code unique carte static int p; //nb de joueurs static int carte_min=0; //carte minimum du tour static int type; //type de tour (simple, double, triple ou quadra) static int selected=0; //nb de carte que le j1 a selection pour jouer static int rang_j1=1; //rang final du joueur 1 static int score_p=0; //score president static int score_n=0; //score neutre static int score_a=0; //score asshole static boolean first = true; //si premiere carte du tour à jouer (=1) static JPanel pan = new JPanel(); //Plateau jeu static Thread menu = new Thread(new Demarrage()); //class Demarrage pour menu static Thread d = new Thread(new Distribution()); //class Distribution pour 3, 4 ou 5 joueurs static Thread u = new Thread(new Player()); //class Player pour choisir le nb de joueurs static Thread pp = new Thread(new Partie_president()); //class Partie_president static Thread fp = new Thread(new Fin_president()); //class Fin_president static ArrayList<Integer> gived = new ArrayList<Integer>(); //cartes distribuées static ArrayList<Integer> gived_j1 = new ArrayList<Integer>(); // cartes du joueur 1 static ArrayList<Integer> gived_j1_i = new ArrayList<Integer>(); // cartes du joueur 1 sans la couleur static ArrayList<Integer> gived_j2 = new ArrayList<Integer>(); // cartes du joueur 2 static ArrayList<Integer> gived_j3 = new ArrayList<Integer>(); // cartes du joueur 3 static ArrayList<Integer> gived_j4 = new ArrayList<Integer>(); // cartes du joueur 4 static ArrayList<Integer> gived_j5 = new ArrayList<Integer>(); // cartes du joueur 5 static ArrayList<Integer> gived_reste = new ArrayList<Integer>(); // cartes en excès: j3=1, j5=2 static ArrayList<Integer> carte_played = new ArrayList<Integer>(); // cartes jouées static ArrayList<JButton> carte_JButton = new ArrayList<JButton>(); //pour desactiver les boutons static ArrayList<JButton> carte_JButton_played = new ArrayList<JButton>(); //pour effacer les cartes au centre static ArrayList<JLabel> dos_array = new ArrayList<JLabel>(); //pour la gestion des dos de carte des bots static ArrayList<JLabel> dos_array_old = new ArrayList<JLabel>(); //pour la gestion des dos de carte des bots //carte des bots en ArrayList de JButton pour faire jouer automatiquement les bots static ArrayList<JButton> carte_bot_j2 = new ArrayList<JButton>(); static ArrayList<JButton> carte_bot_j3 = new ArrayList<JButton>(); static ArrayList<JButton> carte_bot_j4 = new ArrayList<JButton>(); static ArrayList<JButton> carte_bot_j5 = new ArrayList<JButton>(); static Clip clip_president; static Clip clip_main; static Clip clip_win; static Clip clip_loose; static JFrame fenetre_jeu; public static void main(String[] args) throws InterruptedException { try { clip_president = AudioSystem.getClip(); clip_main = AudioSystem.getClip(); clip_win = AudioSystem.getClip(); clip_loose = AudioSystem.getClip(); }catch (Exception error) {} menu.start(); //lancement dos_bot(); } public static void dos_bot() throws InterruptedException{ //dos de carte bot JLabel size_j2 = new JLabel("0"); JLabel size_j3 = new JLabel("0"); JLabel size_j4 = new JLabel("0"); JLabel size_j5 = new JLabel("0"); while(true) { //loop //pour enlever les clignotements dos_array_old.clear(); dos_array_old.addAll(dos_array); dos_array.clear(); if (Main.gived_j2.size()!=0) { for (int j=0; j<Main.gived_j2.size(); j++) { JLabel dos = new JLabel(); dos_array.add(dos); Main.pan.add(dos); dos.setBounds(100+j*20,200,45,63); size_j2.setBounds(75,200,45,63); size_j2.setText(String.valueOf(Main.gived_j2.size())); size_j2.setFont(new Font("Verdana",1,14)); size_j2.setForeground(Color.BLACK); Main.pan.add(size_j2); } if (Main.p==3) { } if (Main.gived_j3.size()!=0) { for (int j=0; j<Main.gived_j3.size(); j++) { JLabel dos = new JLabel(); dos_array.add(dos); Main.pan.add(dos); dos.setBounds(800+j*20,200,45,63); size_j3.setBounds(775,200,45,63); size_j3.setText(String.valueOf(Main.gived_j3.size())); size_j3.setFont(new Font("Verdana",1,14)); size_j3.setForeground(Color.BLACK); Main.pan.add(size_j3); } } } if (Main.p==4) { if (Main.gived_j3.size()!=0) { for (int j=0; j<Main.gived_j3.size(); j++) { JLabel dos = new JLabel(); dos_array.add(dos); Main.pan.add(dos); dos.setBounds(300+j*20,80,45,63); size_j3.setBounds(275,80,45,63); size_j3.setText(String.valueOf(Main.gived_j3.size())); size_j3.setFont(new Font("Verdana",1,14)); size_j3.setForeground(Color.BLACK); Main.pan.add(size_j3); } } if (Main.gived_j4.size()!=0) { for (int j=0; j<Main.gived_j4.size(); j++) { JLabel dos = new JLabel(); dos_array.add(dos); Main.pan.add(dos); dos.setBounds(800+j*20,200,45,63); size_j4.setBounds(775,200,45,63); size_j4.setText(String.valueOf(Main.gived_j4.size())); size_j4.setFont(new Font("Verdana",1,14)); size_j4.setForeground(Color.BLACK); Main.pan.add(size_j4); } } } else if (Main.p==5) { if (Main.gived_j3.size()!=0) { for (int j=0; j<Main.gived_j3.size(); j++) { JLabel dos = new JLabel(); dos_array.add(dos); Main.pan.add(dos); dos.setBounds(200+j*20,80,45,63); size_j3.setBounds(175,80,45,63); size_j3.setText(String.valueOf(Main.gived_j3.size())); size_j3.setFont(new Font("Verdana",1,14)); size_j3.setForeground(Color.BLACK); Main.pan.add(size_j3); } } if (Main.gived_j4.size()!=0) { for (int j=0; j<Main.gived_j4.size(); j++) { JLabel dos = new JLabel(); dos_array.add(dos); Main.pan.add(dos); dos.setBounds(700+j*20,80,45,63); size_j4.setBounds(675,80,45,63); size_j4.setText(String.valueOf(Main.gived_j4.size())); size_j4.setFont(new Font("Verdana",1,14)); size_j4.setForeground(Color.BLACK); Main.pan.add(size_j4); } } if (Main.gived_j5.size()!=0) { for (int j=0; j<Main.gived_j5.size(); j++) { JLabel dos = new JLabel(); dos_array.add(dos); Main.pan.add(dos); dos.setBounds(800+j*20,200,45,63); size_j5.setBounds(775,200,45,63); size_j5.setText(String.valueOf(Main.gived_j5.size())); size_j5.setFont(new Font("Verdana",1,14)); size_j5.setForeground(Color.BLACK); Main.pan.add(size_j5); } } } dos_array.forEach(n -> n.setIcon(new ImageIcon("..\\Paquet de carte\\dos.png"))); dos_array_old.forEach(n -> Main.pan.remove(n)); Main.pan.updateUI(); Thread.sleep(1000); } } }
Code:
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
200
201
202
203
204
205 import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.io.*; import sun.audio.*; import javax.sound.sampled.*; //import java.io.IOException; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JButton; public class Distribution extends JFrame implements Runnable{ //info thread: http://www.tutorialspoint.com/java/lang/thread_join.htm public void run() { Main.clip_main.stop(); //arret de la musique du menu try { //musique principale du jeu File file = new File("president_song.wav"); Main.clip_president.open(AudioSystem.getAudioInputStream(file)); Main.clip_president.start(); Main.clip_president.loop(Main.clip_president.LOOP_CONTINUOUSLY); //lecture en boucle }catch (Exception error) {} Main.fenetre_jeu = this; Main.fenetre_jeu.setTitle("Plateau de Jeu"); Main.fenetre_jeu.setSize(1200, 700); Main.fenetre_jeu.setLocationRelativeTo(null); Main.fenetre_jeu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); do { for (int n=0; n<52; n++) { String[] possibilities = { "h", "c", "d", "s" }; int a,i; do { a = (int)(Math.random()*4); //(generer un nombre aleatoire entre 0 et 3) i = (int)(Math.random()*13)+2; //(generer un nombre aleatoire entre 2 et 14) Main.z=a*100+i; //"chiffre unique" pour identifier chaque carte } while(Main.gived.contains(Main.z)==true); Main.gived.add(Main.z); //envoie du "chiffre unique" dans la liste "gived" JButton carte = new JButton(new ImageIcon("..\\Paquet de carte\\" + i + possibilities[a] + ".gif")) ; //extraire la carte Main.carte_JButton.add(carte); Main.pan.add(carte); //afficher la carte Main.pan.setLayout(null); if (Main.p==5 && (n==50||n==51)) { //separer les cartes en excès Main.gived_reste.add(Main.z); carte.setLocation((n-50)*70,0); } else if (Main.p==3 && n==51) { //separer les cartes en excès Main.gived_reste.add(Main.z); carte.setLocation((n-51)*70,0); } else if (n%Main.p==0) { Main.gived_j1.add(Main.z); Main.gived_j1_i.add(i); if (n<26) carte.setLocation(100+(n-1)*40,500); else carte.setLocation(60+(n-27)*40,550); } else if (n%Main.p==1) { Main.gived_j2.add(Main.z); Main.carte_bot_j2.add(carte); //carte.setLocation(200,50+(n*5)); } else if (n%Main.p==2){ Main.gived_j3.add(Main.z); Main.carte_bot_j3.add(carte); //carte.setLocation(400,n*5); } else if (n%Main.p==3){ Main.gived_j4.add(Main.z); Main.carte_bot_j4.add(carte); //carte.setLocation(800,(n*5)); } else { Main.gived_j5.add(Main.z); Main.carte_bot_j5.add(carte); //carte.setLocation(1000,50+(n*5)); } Main.fenetre_jeu.setContentPane(Main.pan); Main.fenetre_jeu.setVisible(true); carte.setSize(71, 100); carte.setContentAreaFilled(false); //pas de remplissage carte.setBorderPainted(false); //pas de bordure carte.setFocusPainted(false); //pas de focus carte.setName(String.valueOf(Main.z)); //convert° en string Main.pan.setBackground(Color.LIGHT_GRAY); System.out.println("carte:"+(n+1)+" a="+a+" i="+i); try { Thread.sleep(100); } catch (InterruptedException e1) {} carte.addActionListener(new ActionListener(){ // debut listener à chaque carte public void actionPerformed(ActionEvent e) { int a = Integer.parseInt(carte.getName()); if (Main.selected!=Main.type && Main.x==1) { if (Main.gived_j1.contains(a) && (Main.carte_min<=a%100||a%100==2) ) { //convert° en int //et >cmin if (Main.selected>0) { if (a%100==Main.carte_played.get(Main.selected-1)%100) { Main.carte_played.add(a); //pour supprimer de gived et gived_j1 Main.carte_JButton_played.add(carte); //pour supprimer à la fin du tour de j1 carte.setLocation(600+(71*Main.selected),300); System.out.println(Main.selected); Main.selected++; Main.gived_j1.removeAll(Main.carte_played); Main.gived.removeAll(Main.carte_played); } } else { //si premiere carte, verification que le choix correspond au type de tour choisi int verif=0; for (int i = 0; i < Main.gived_j1_i.size(); i++) { if (Main.gived_j1_i.get(i)==a%100) { verif++; } } if (verif>=Main.type) { Main.carte_min=a%100; System.out.println("valide pour type"); Main.selected++; Main.carte_played.add(a); //pour supprimer de gived et gived_j1 Main.carte_JButton_played.forEach(n -> Main.pan.remove(n)); //supprimer du centre les cartes jouées (par les bots) Main.pan.updateUI(); Main.carte_JButton_played.add(carte); //pour supprimer à la fin du tour de j1 carte.setLocation(600,300); Main.gived_j1.removeAll(Main.carte_played); Main.gived.removeAll(Main.carte_played); } } } } System.out.println("Carte touché: "+carte.getName()); } }); //fin du listener des cartes } JLabel commence = new JLabel(); commence.setBounds(0, 0, 1200, 50); Main.pan.add(commence); //afficher le texte if (Main.gived_j1.contains(12)) { //recherche dame de coeur commence.setLocation(500,400); Main.x=1; } else if (Main.gived_j2.contains(12)){ commence.setLocation(200,200); Main.x=2; } else if (Main.gived_j3.contains(12)){ commence.setLocation(400,300); Main.x=3; } else if (Main.gived_j4.contains(12)){ commence.setLocation(800,300); Main.x=4; } else if (Main.gived_j5.contains(12)){ commence.setLocation(900,500); Main.x=5; } else { commence.setLocation(200,450); Main.x=0; } if (Main.x==0) { commence.setText("Aucun joueur ne possède la dame de coeur, les cartes vont être redistribuées"); commence.setFont(new Font("Verdana",1,20)); try { Thread.sleep(3000); } catch (InterruptedException e1) {} Main.gived.clear(); Main.gived_j1.clear(); Main.gived_j2.clear(); Main.gived_j3.clear(); Main.gived_j4.clear(); Main.gived_j5.clear(); Main.gived_reste.clear(); Main.pan.removeAll(); } else { commence.setText("Le joueur "+Main.x+" commence"); commence.setFont(new Font("Verdana",1,20)); Main.gived.removeAll(Main.gived_reste); //enlever les cartes restantes des cartes en jeu System.out.println(Main.gived.size()+" cartes distribuées"); Main.pan.remove(commence); Main.pp.start(); } } while (Main.x==0); } }