bonjour à tous
je ne sais pas si je suis dans le bon forum
mon objectif est de programmer une interface java qui affiche l'ensemble du contenu d'un fichier XML générer par le modèle conceptuel MADS
quand j'exécute mon code avec un exemple d'un fichier XML simple sa marche très bien ,mais quand je lis un ficher xml générer pas MADS , une très grande erreur s'affiche
voici 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 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at affiche.<init>(affiche.java:41) at Fenetre$Ouvrir.actionPerformed(Fenetre.java:94) 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.AbstractButton.doClick(Unknown Source) at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source) at javax.swing.plaf.basic.BasicMenuItemUI$Handler.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)
et voici mon code
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 import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JTabbedPane; public class Fenetre extends JFrame{ public JPanel container = new JPanel(); private JMenuBar menuBar = new JMenuBar(); public JPanel pan=new JPanel(); //****************************les menus********************************// JMenuItem ouvrir = new JMenuItem("ouvrir"); private JMenu file= new JMenu("File"); JTabbedPane anglet = new JTabbedPane(); public Fenetre(){ anglet.addTab("MADS", null, pan, "anglet pour afficher le contenu du fichier MADS"); anglet.setTitleAt(0,"CONTENU DE MADS"); //ouvrir.addActionListener(new chercher_xml()); //pan.setLayout(new FlowLayout()); this.setTitle("test"); this.setSize(700, 600); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); BorderLayout flowLayout1= new BorderLayout(); container.setBackground(Color.white); container.setLayout(new BorderLayout()); this.getContentPane().setLayout(flowLayout1); container.add(anglet,null); this.setContentPane(container); this.initMenu(); this.setVisible(true); } private void initMenu(){ file.add(ouvrir); ouvrir.addActionListener(new Ouvrir()); menuBar.add(file); this.setJMenuBar(menuBar);} class Ouvrir implements ActionListener{ public File f_choose; JLabel f=new JLabel(); public void actionPerformed(ActionEvent evt) { JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new File(".")); chooser.setFileFilter(new javax.swing.filechooser.FileFilter() { public boolean accept(File f) {f_choose=f; return f.getName().toLowerCase().endsWith(".xml") || f.isDirectory(); } public String getDescription() { return "fichier xml"; } }); int n = chooser.showOpenDialog(null); if (n == JFileChooser.APPROVE_OPTION) { String name1 = chooser.getSelectedFile().getName(); name1=chooser.getSelectedFile().getAbsolutePath(); System.out.println(name1); f_choose=chooser.getSelectedFile(); anglet.setSelectedIndex(0); affiche affi=new affiche((JPanel)anglet.getSelectedComponent(),name1); }}} public static void main(String[] args) { Fenetre FEN = new Fenetre(); } }
merci à vous pour toute aide
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 import java.awt.BorderLayout; import java.awt.FlowLayout; import java.io.File; import java.util.Iterator; import java.util.List; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.border.TitledBorder; import org.jdom.*; import org.jdom.input.*; import org.jdom.output.XMLOutputter; import org.jdom.filter.*; public class affiche { static org.jdom.Document document; static Element racine; static Element element; public static String text = new String (); public String text3=""; public affiche (JPanel pan , String name){ String name1=name; SAXBuilder sxb = new SAXBuilder(); try { //On crée un nouveau document JDOM avec en argument le fichier XML //Le parsing est terminé ;) document = sxb.build(new File(name)); } catch(Exception e){} //On initialise un nouvel élément racine avec l'élément racine du document. racine = document.getRootElement(); List childs =racine.getChildren(); Iterator iter = childs.iterator() ; JTextArea ttt = new JTextArea(); JScrollPane scro = new JScrollPane(); scro.setBorder(new TitledBorder("mon espace ")); pan.setLayout(new BorderLayout()); while(iter.hasNext()) { Element courant = (Element)iter.next(); System.out.println(courant.getAttributeValue("classe")); String text=courant.getAttributeValue("classe"); text3= text.concat(text3); ttt.setText(""+text3); } scro=new JScrollPane(ttt); pan.add(scro,null); } }![]()
Partager