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

AWT/Swing Java Discussion :

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


Sujet :

AWT/Swing Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé Avatar de hamham
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    80
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 80
    Par défaut Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    Bonjour à tous,

    je travaille en ce moment sur un projet en java en théroie des graphes (le métro). J'ai fait une interface grapghique pour pouvoir afficher mon graphe. Le soucis c'est que lorsque je veux recharger ma page, en remettant les stations à leur état initial, j'obtient cette 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
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at metro.Graphe.resetStation(Graphe.java:446)
    	at metro.Menu$2.actionPerformed(Menu.java:86)
    	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.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)
    Voici la classe de mon interface
    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
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import javax.swing.*;
    import java.util.*;
     
    public class Menu extends JPanel{
     
    	private final Graphe graphe;
    	JComboBox cItiOri;
    	JComboBox cItiDest;
     
    	public Menu(final Fenetre fen, Graphe g)
    	{
    		graphe=g;
    		this.setBackground(Color.lightGray);
    		setSize(200, 600);
    		setLayout(new FlowLayout(FlowLayout.CENTER));
    		Box vBox = Box.createVerticalBox();
    		Box hBox = Box.createHorizontalBox();
    		Box hBox1 = Box.createHorizontalBox();
    		Box hBox2 = Box.createHorizontalBox();
    		Box hBox3 = Box.createHorizontalBox();
     
    		JLabel ligne = new JLabel("Situer une ligne");
    		hBox.add(ligne);
    		hBox.add(Box.createHorizontalGlue());
     
    		Vector listeLigne = getListLigne();
    		JComboBox cLigne = new JComboBox(listeLigne);
    		cLigne.addActionListener(new selectLigne());
     
    		hBox.add(cLigne);
     
    		vBox.add(hBox);
    		vBox.add(Box.createVerticalStrut(30));
     
    		JLabel station = new JLabel("Situer une station");
    		hBox1.add(station);
    		hBox1.add(Box.createHorizontalGlue());
     
    		Vector listeStation = getListStation();
    		JComboBox cStation = new JComboBox(listeStation);
    		cStation.addActionListener(new selectStation());
    		hBox1.add(cStation);
     
    		vBox.add(hBox1);
    		vBox.add(Box.createVerticalStrut(30));
     
    		JLabel itineraire = new JLabel("Itinéraire : ");
    		vBox.add(itineraire);
     
    		cItiOri = new JComboBox(listeStation);
    		hBox2.add(cItiOri);
    		hBox2.add(Box.createHorizontalStrut(5));
     
    		cItiDest = new JComboBox(listeStation);
    		hBox2.add(cItiDest);
     
    		vBox.add(hBox2);
    		vBox.add(Box.createVerticalStrut(30));
     
    		JButton bOk = new JButton("Validez");
    		bOk.addActionListener(new ActionListener()
    		{
    			public void actionPerformed (ActionEvent evenement)
    			{
    				String ori = cItiOri.getSelectedItem().toString();
    				String dest = cItiDest.getSelectedItem().toString();
    				Graphe.chemin(graphe, Graphe.getStation(ori).get_num(), Graphe.getStation(dest).get_num());
    			}
    		});
    		vBox.add(bOk);
    		vBox.add(Box.createVerticalStrut(60));
     
    		JButton bReset = new JButton("Recharger");
    		bReset.addActionListener(
    				new ActionListener()
    				{
    					public void actionPerformed (ActionEvent evenement)
    					{
    					Graphe.resetStation();
     
     
    					}
    				}
    			);
    		hBox3.add(bReset);
    		hBox3.add(Box.createHorizontalStrut(5));
     
     
    		JButton bLignes = new JButton("changer de fichier");
    		bLignes.addActionListener(
    				new ActionListener()
    				{
    					public void actionPerformed (ActionEvent evenement)
    					{
    						String[] fichier = {"lignes.data", "lignes2.data", "lignes3.data", "lignes4.data",
    						"lignes5.data", "lignes6.data"};
    						String fic = (String)JOptionPane.showInputDialog (fen, "Choisissez votre fichier d'entrée",
    						"Choix du fichier en entrée", //titre de la boîte
    						JOptionPane.QUESTION_MESSAGE, null, fichier,fichier[0]);
     
    						graphe.changementFichier(fic);
     
    					}
    				}
    			);
    		hBox3.add(bLignes);
    		vBox.add(hBox3);
     
    		this.add(vBox);
     
    	}

  2. #2
    Membre confirmé Avatar de hamham
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    80
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 80
    Par défaut
    Excusez moi je n'avais pas terminé,

    Voici les parties intéréssantes (pour le sujet ) de ma classe Graphe :
    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
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
     
    public class Graphe {
     
    	static int taillemax;
    	static Station listeStation[];
    	Successeur listeSucc[];
    	static Fenetre fen;
     
    	public Graphe(int taille, String fic)
    	{
    		taillemax=taille;
    		listeSucc = new Successeur[taille];
     
    		int i;
    		for(i=0; i<taille; i++)
    		{
    			listeSucc[i]=null;
    		}
     
    		listeStation = new Station[taille];
    		for(i=0; i<taille; i++)
    		{
    			listeStation[i]=null;
    		}
     
    		marque = new boolean[taillemax] ;
    		this.initStation();
     
    		this.initGraphe(fic);
     
    		pere = new int[taillemax]; 
    		for(i=0; i<taille; i++)
    		{
    			pere[i] = -1;
    		}
    		dessiner();
    	}
     
     
     
     
    	public void dessiner()
    	{
    		fen = new Fenetre("Plan du métro parisien", this);
    		fen.setSize(1300,600); 
    		fen.setVisible(true);
    		System.out.println("dessin terminé");
     
    	}
    	private void initStation()
    	{
    		FileReader fisLigne=null;
    		String line="", ligneMetro="", nomStation="";
    		Successeur suiv=null;
    		int x, y, i=1;
    		Station stat;
     
    		try
    		{
    			fisLigne = new FileReader("docs/stations.data");
    			BufferedReader dataInLigne = new BufferedReader(fisLigne);
     
    			while((line=dataInLigne.readLine())!=null)
    			{
    				StringTokenizer test = new StringTokenizer(line, " ");
     
    				x = Integer.parseInt(test.nextToken().toString());
    				y = Integer.parseInt(test.nextToken().toString());
    				nomStation=line.substring(8);
    				ligneMetro = "ligne"+Fichier.getLigne(nomStation);
     
    				stat=new Station(i, nomStation,x, y, ligneMetro);
    				listeStation[i]=stat;
    				i++;
    			}
    			fisLigne.close();
    			dataInLigne.close();
     
     
    			/* on ferme le FileReader et le buffer */
     
    		}
    		catch(FileNotFoundException e)
    		{
    			System.out.println("erreur sur le fichier");
    			e.printStackTrace();
    		}
    		catch (IOException ioe)
    		{
    			System.out.println("IO problème");
    			System.out.println(ioe.getMessage());
    		}
    		catch (Exception ex)
    		{
    			System.out.println("erreur générale");
    			ex.printStackTrace();
    			System.out.println(ex.getMessage());
    		}
     
    	}
     
    	private void initGraphe(String fic)
    	{
    		FileReader fisLigne=null;
    		String line="", ligneMetro="", stationLigne="";
    		Successeur suiv=null;
     
    		try
    		{
    			/* on créé un Buffer de lecture grâce à l'objet BufferedReader 
    			 * qui prend en entrée un FileReader qui ouvre le fichier à coder */
     
     
    			/* grâce au buffer on lit le fichier ligne par ligne*/
    			for(int i =1; i<taillemax; i++)
    			{
    				//successeurs de la station à la ligne i
    				String station = Fichier.rechercheNomStation(i);
     
    				fisLigne = new FileReader("docs/"+fic);
    				BufferedReader dataInLigne = new BufferedReader(fisLigne);
     
    				while((line=dataInLigne.readLine())!=null)
    				{
    					if(!(line.charAt(0)=='#'))
    					{
    						StringTokenizer test = new StringTokenizer(line, ":");
     
    						ligneMetro = test.nextToken();
    						stationLigne = test.nextToken();
     
    						if(station.equalsIgnoreCase(stationLigne))
    						{
    							stationLigne = dataInLigne.readLine();
    							if(stationLigne!=null && !(stationLigne.substring(0, 4)).equalsIgnoreCase("####"))
    							{
    								StringTokenizer test2 = new StringTokenizer(stationLigne, ":");
    								ligneMetro=test2.nextToken();
    								stationLigne = test2.nextToken();
     
    								int numStationSuiv = Fichier.rechercheNumStation(stationLigne);
    								suiv = new Successeur(numStationSuiv, ligneMetro, null);
    								suiv.setSuivant(listeSucc[i]);
    								listeSucc[i]=suiv;
    							}
    						}
    					}
    				}
    				fisLigne.close();
    				dataInLigne.close();
    			}
     
    			/* on ferme le FileReader et le buffer */
     
    		}
    		catch(FileNotFoundException e)
    		{
    			System.out.println("erreur sur le fichier");
    			e.printStackTrace();
    		}
    		catch (IOException ioe)
    		{
    			System.out.println("IO problème");
    			System.out.println(ioe.getMessage());
    		}
    		catch (Exception ex)
    		{
    			System.out.println("erreur générale");
    			ex.printStackTrace();
    			System.out.println(ex.getMessage());
    		}
     
    	}
     
     
     
     
    /* ***************************** Dessin ********************************* */
    			static void dessinStation(String nomStation)
    			{
    				System.out.println("je suis dans dessinStation");
    				int numStation = Fichier.rechercheNumStation(nomStation);
    				System.out.println("num station : "+numStation);
    				Station stat = Graphe.getStation(numStation);
    				stat.setDiametre(6);
    				stat.setCoul(Color.red);
    				synchronized (listeStation) {
    					fen.plan.repaint();
    				}
     
    			}
     
    			static void resetStation() 
    			{
    				for(int i= 0; i<taillemax; i++)
    				{
    					if(listeStation[i].getCoul()!=Color.black || listeStation[i].getDiametre()!=2)
    					{
    						listeStation[i].setCoul(Color.black);
    						listeStation[i].setDiametre(2);
    					}
    				}
     
     
    			}
     
     
    			public void changementFichier(String fic)
    			{
    				int i;
    				for(i=0; i<taillemax; i++)
    				{
    					listeSucc[i]=null;
    				}
     
    				initGraphe(fic);
    				fen.plan.repaint();
    			}
     
    			public static Station getStation(String nom)
    			{
    				int i=1;
    				while(i<taillemax && !nom.equalsIgnoreCase(listeStation[i].getNom()))
    				{
    					i++;
    				}
    				if(i==taillemax)return null;
    				return listeStation[i];
    			}
    }
    le problème estquand je clique sur le bouton recharger, j'ai l'impression que la mise à jour du tableau des stations se fait en meme temps que le dessin. Du coup lorsque le dessin (paint()) arrive sur une station non mise à jour, il lance une exception.

    Quelqu'un pourrait m'aider s'il vous plait......

    Merci d'avance

  3. #3
    Membre Expert
    Avatar de CheryBen
    Inscrit en
    Mai 2005
    Messages
    1 599
    Détails du profil
    Informations personnelles :
    Âge : 43

    Informations forums :
    Inscription : Mai 2005
    Messages : 1 599
    Par défaut
    Bonjour, c'est listStation[i] qui vaut null, il faudrait savoir pourquoi, tu peux ajouter des traces pour avoir plus d'infos sur l'initialisation du tableau, quelle station est nulle.

    Mieu que les traces, un debugger, celui d'eclipse par exemple

  4. #4
    Membre confirmé Avatar de hamham
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    80
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 80
    Par défaut
    C'était listeStation[0] qui était nulle donc c'est bon j'ai trouvé mon erreur grâce à toi. Bon j'en ai pas mal d'autre à trouver mais c'est dejà ça

    Un grand merci

  5. #5
    Membre Expert
    Avatar de CheryBen
    Inscrit en
    Mai 2005
    Messages
    1 599
    Détails du profil
    Informations personnelles :
    Âge : 43

    Informations forums :
    Inscription : Mai 2005
    Messages : 1 599
    Par défaut
    Ok, un grand de rien

    Pense à

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 4
    Dernier message: 08/02/2010, 08h23
  2. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException ?
    Par freezerhm dans le forum Concurrence et multi-thread
    Réponses: 5
    Dernier message: 04/12/2007, 09h26
  3. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    Par Trint dans le forum Interfaces Graphiques en Java
    Réponses: 6
    Dernier message: 27/02/2007, 11h28
  4. Réponses: 8
    Dernier message: 11/05/2006, 19h32
  5. [JDIC]Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    Par kedare dans le forum Concurrence et multi-thread
    Réponses: 4
    Dernier message: 06/05/2006, 22h45

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