Bonjour,
j'ai vu quelques situations impliquant la même erreur, mais les situations sont toujours un peu différentes et je n'ai pas réussi à me dépanner.
Je fais présentement une interface qui permet d'afficher du texte dans un JTextArea (j'essaie du moins...) et j'ai des problèmes lorsque j'essaie d'ouvrir le texte. En effet, je reçois toujours l'exception java.lang.NullPointerException. Voici mon code:
Il est a noter que j'ai remplacé l'exception dans le catch du try/catch pour qu'on puisse voir l'erreur sur la console. Habituellement, je mettais seulement Exception pour enlever les IOException et les exceptions que pourraient causer un mauvais nom de fichier. Il y a encore trois menus non-utilisés que j'aimerais exploiter éventuellement. Je les ai mis en commentaire donc ne vous en occupez pas.
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 import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; import javax.swing.JFileChooser; import javax.swing.JMenuBar; import javax.swing.JMenu; import javax.swing.JMenuItem; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JLabel; import javax.swing.LayoutStyle.ComponentPlacement; import javax.swing.JTextField; public class BaseGui extends JFrame { private JPanel contentPane; private JTextField textRechercher; private JTextArea textProlog; /** * Lancement de l'application */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { BaseGui frame = new BaseGui(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Initialisation */ public BaseGui() { initComposants(); } public void initComposants() { setTitle("Interface graphique Prolog"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 674, 510); JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); JMenu mnFichier = new JMenu("Fichier"); menuBar.add(mnFichier); JMenuItem mnOuvrirOrdi = new JMenuItem("Ouvrir un fichier sur l'ordinateur"); mnFichier.add(mnOuvrirOrdi); JMenuItem mnOuvrirServeur = new JMenuItem("Ouvrir un fichier sur un serveur"); mnFichier.add(mnOuvrirServeur); JMenuItem mnSauvegardeOrdi = new JMenuItem("Sauvegarder un fichier sur l'ordinateur"); mnFichier.add(mnSauvegardeOrdi); JMenuItem mnSauvegardeServeur = new JMenuItem("Sauvegarder un fichier sur un serveur"); mnFichier.add(mnSauvegardeServeur); JMenuItem mnQuitter = new JMenuItem("Quitter"); mnQuitter.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { dispose(); } }); mnFichier.add(mnQuitter); JMenu mnAide = new JMenu("Aide"); menuBar.add(mnAide); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); JScrollPane scrollPane = new JScrollPane(); JLabel lblRecherche = new JLabel("Recherche :"); textRechercher = new JTextField(); textRechercher.setColumns(10); GroupLayout gl_contentPane = new GroupLayout(contentPane); gl_contentPane.setHorizontalGroup( gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addContainerGap() .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 627, GroupLayout.PREFERRED_SIZE) .addGroup(gl_contentPane.createSequentialGroup() .addComponent(lblRecherche) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(textRechercher, GroupLayout.DEFAULT_SIZE, 566, Short.MAX_VALUE))) .addContainerGap()) ); gl_contentPane.setVerticalGroup( gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addContainerGap() .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 368, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED, 26, Short.MAX_VALUE) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(lblRecherche) .addComponent(textRechercher, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addGap(22)) ); textProlog = new JTextArea(); scrollPane.setViewportView(textProlog); contentPane.setLayout(gl_contentPane); mnOuvrirOrdi.addActionListener(new OpenL()); // mnOuvrirServeur.addActionListener(new OpenD()); // mnSauvegardeOrdi.addActionListener(new SaveL()); // mnSauvegardeServeur.addActionListener(new SaveD()); } class OpenL implements ActionListener { public void actionPerformed(ActionEvent e) { JFileChooser ouverture = new JFileChooser(); try{ String strLine; File selectedFile = ouverture.getSelectedFile(); FileInputStream in = new FileInputStream(selectedFile); BufferedReader br = new BufferedReader(new InputStreamReader(in)); while ((strLine = br.readLine()) != null) { textProlog.append(strLine + "\n"); } }catch(IOException e1){ textProlog.append("Impossible d'afficher ce ficher."); } } } }
Voici le message d'erreur:
Avez-vous des idées?Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
IOException cannot be resolved to a type
at jfilechooser.resources.BaseGui$OpenL.actionPerformed(BaseGui.java:145)
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.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$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)
Partager