Bonjour,
J"utilise ce code issu de différents topic du forum pour analyser un fichier texte puis afficher un msg dans un Systray
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
public class vue extends javax.swing.JFrame {
 private TrayIcon tray;
 private FolderSpyWorker folderSpyWorker=new FolderSpyWorker("d://Games//");
 Thread folderSpyThread = new Thread(folderSpyWorker);
 
    public vue() throws AWTException {
      initComponents();
      test();
    }
void test(){
      try {
 
            folderSpyThread.start();         
 
            folderSpyThread.interrupt();  
 
        } catch (Exception ex) {
            Logger.getLogger(vue.class.getName()).log(Level.SEVERE, null, ex);
        }
}
  private void formWindowIconified(java.awt.event.WindowEvent evt) {                                     
                setExtendedState(Frame.ICONIFIED);
		setVisible(false);
		System.out.println("fentre en icone");
		iconTray();        // TODO add your handling code here:
    }                                    
 
    private void formWindowDeiconified(java.awt.event.WindowEvent evt) {                                       
                setExtendedState(Frame.NORMAL);
                SystemTray.getSystemTray().remove(tray);
		System.out.println("icone en fenetre");        // TODO add your handling code here:
    }                                      
public void Actipeche(String chemino) throws IOException{
         String TextAis = "";
         String fichierContenu,erreur;
           int cpt = 0;
           erreur = "";
           fichierContenu = ouvrir(chemino);
           String textecrire;
           int i = 0;
           Pecheur pecheposit =new Pecheur();
           ConvertionPosit convertposit = new ConvertionPosit();
        try {
            BufferedReader in4 = new BufferedReader(new StringReader(fichierContenu));
            String s = null;
            while((s = in4.readLine()) != null ){
               Pattern pattern = Pattern.compile(
			"(\\d+)" +	// des chiffres (groupe 1)
			"/" +		// un slash
			"(\\d+" +	// des chiffres (groupe 2)
			"[A-Z])-" +	// Une lettre majuscule suivi d'un tiret
			"(\\d+" +	// des chiffres (groupe 3)
			"[A-Z])");	// Une lettre majuscule
 
	Matcher m = pattern.matcher(s);
	if (m.matches()) {
            String t = String.valueOf(i); 
            int a = Integer.parseInt(m.group(1) + t);
           textecrire = pecheposit.posEncodemsg4(convertposit.renvoilatdegmin(m.group(2)), convertposit.renvoilongdegmin(m.group(3)), a);
           envoiReseau(textecrire, 6020);
                 i++;
	} else {
		String t = s.split("/")[0];
                   Pattern MY_PATTERN = Pattern.compile("(\\d+){1,2}");
                    Matcher m3 = MY_PATTERN.matcher(t);
                    if (m3.matches()) {
                      erreur += s + "\n" ;   
                    }
 
	}       
            }
            cpt = i;
           // 
           //if(TextAis!="") ecrire(Ch_Acti + "\\actipeche.txt", TextAis);
       }  catch (SixbitException ex) {
            Logger.getLogger(JTimAuto.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(JTimAuto.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            Logger.getLogger(JTimAuto.class.getName()).log(Level.SEVERE, null, ex);
        } 
 
            System.out.println("ici");
            affichemsg(cpt);
 
    } 
void affichemsg(int nb){
    tray.displayMessage("essai", "Vous avez créer" + nb + "  positions de pecheurs", TrayIcon.MessageType.INFO);
 
}
private void iconTray() {
		if (SystemTray.isSupported()) {
			System.out.println("SysTray supported");
			// construction du menu et gestion des événements
			PopupMenu popup = new PopupMenu();
			MenuItem démarrer = new MenuItem("Afficher");
			MenuItem quitter = new MenuItem("Quitter");
			ActionListener afficher = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					setVisible(true);
					setExtendedState(Frame.NORMAL);
 
				}
			};
			ActionListener arrêter = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						tray.displayMessage("Arrêt de l'application Essai",
								"A bientôt", TrayIcon.MessageType.INFO);
						Thread.sleep(4000);
					} catch (InterruptedException ex) {
					} finally {
						System.exit(0);
					}
				}
			};
			démarrer.addActionListener(afficher);
			quitter.addActionListener(arrêter);
			popup.add(démarrer);
			popup.add(quitter);
			// création de l'icône
			Image icone = Toolkit.getDefaultToolkit().getImage(getClass().getResource("nuclearsubmarine.png"));// Si l'image est dans le jar //$NON-NLS-1$
			tray = new TrayIcon(icone,"XXXXXX", popup);
			System.out.println("Affichage de l'icone");
			tray.setImageAutoSize(true);
			tray.addActionListener(afficher);
			// placement de l'icône dans la barre de tâche
			try {
				SystemTray.getSystemTray().add(tray);
			} catch (AWTException ex) {
			}
		}
	}
 void envoiReseau(String message,int Port) throws SocketException, UnknownHostException, IOException{
     int tutu = 1; 
     if(tutu == 1){
      BufferedReader inFromUser =
      new BufferedReader(new InputStreamReader(System.in));
      DatagramSocket clientSocket = new DatagramSocket();
      InetAddress IPAddress = InetAddress.getByName("127.0.0.1");
      byte[] sendData = new byte[1024];
      String sentence = message;
      sendData = sentence.getBytes();
      DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, Port);
      clientSocket.send(sendPacket);
      clientSocket.close();
      }
    }   
 String ouvrir(String chemi) throws IOException{
     RandomAccessFile raf = new RandomAccessFile(chemi, "r");
        String fichierC = "";       
                try
                    {
 
 
                    String ligne;
                        while ( (ligne = raf.readLine()) != null )
                          {
                          fichierC += ligne + "\n";
                          }
 
                        }
                        catch (IOException e)
                          {
                          JOptionPane.showMessageDialog(this, "Aucun fichier spécifié!");
                          }finally{
                          raf.close();
                }
 
       return fichierC;
    }
Tout ce passe bien sauf pour "affichemsg" qui me crée
déc. 27, 2013 2:35:47 PM jtimauto.FolderSpyWorker run
SEVERE: null
java.lang.NullPointerException
at jtimauto.vue.affichemsg(vue.java:180)
at jtimauto.vue.Actipeche(vue.java:176)
at jtimauto.FolderSpyWorker.run(FolderSpyWorker.java:56)
at java.lang.Thread.run(Thread.java:722)
Voici la classe FolderSpyWorker
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
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.logging.Level;
import java.util.logging.Logger;
 
/**
 * Surveiller les changements dans un dossier
 * @author Axel
 * @see http://www.fobec.com/java/1126/detecter-changements-fichiers-dans-dossier.html
 */
public class FolderSpyWorker implements Runnable {
 
    private Path path = null;
 
    /**
     * Constructor
     * @param pathname String dossier à surveiller
     */
    public FolderSpyWorker(String pathname) {
        this.path = Paths.get(pathname);
        System.out.println("Start FolderSpy on:" + pathname);
    }
 
    /**
     * Worker
     */
    public void run() {
        WatchService watchService = null;
        try {
            watchService = this.path.getFileSystem().newWatchService();
            //Enregistrer les opérations à surveiller
            this.path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
                    StandardWatchEventKinds.ENTRY_MODIFY,
                    StandardWatchEventKinds.ENTRY_DELETE);
 
            WatchKey watchKey;
            // boucle sans fin
            while (!Thread.interrupted()) {
                watchKey = watchService.take();
 
                //traiter les evenements
                for (WatchEvent<?> event : watchKey.pollEvents()) {
                    String fileName = event.context().toString();
                    if (StandardWatchEventKinds.ENTRY_CREATE.equals(event.kind())) {
                        vue mavue = new vue();
                        mavue.Actipeche(path +"\\"+ fileName);
                    } else if (StandardWatchEventKinds.ENTRY_MODIFY.equals(event.kind())) {
                        System.out.println(fileName+" has been modified");
                    } else if (StandardWatchEventKinds.ENTRY_DELETE.equals(event.kind())) {
                        System.out.println(fileName+" has been deleted");
                    } else if (StandardWatchEventKinds.OVERFLOW.equals(event.kind())) {
                        System.out.println("Strange event");
                        continue;
                    }
                }
                //se place en attente de message
 
                watchKey.reset();
            }
        } catch (InterruptedException ex) {
            try {
                if (watchService!=null) watchService.close();
 
                System.out.println("Stop FolderSpy");
            } catch (IOException ex1) {
                Logger.getLogger(FolderSpyWorker.class.getName()).log(Level.SEVERE, null, ex1);
            }
        } catch (Exception ex) {
            Logger.getLogger(FolderSpyWorker.class.getName()).log(Level.SEVERE, null, ex);
        }
    }  
}
Si quelqu'un peut m'aiguiller. Merci