Je travaille sur un projet de robotique
J'utilise Netbeans IDE 6.0.1
Donc voila je souhaite faire un thread pour pouvoir récupérer la position de mon robot

Donc voici se que je fait :

Dans ma classe CCommunication:


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
 
public class CCommunication implements Runnable{
 
 
 protected  String a;
    MyFenetre f;
 private BufferedImage destinationImage;
  CSysteme sys = new CSysteme(f);
  int lu;
 
public CCommunication(CSysteme sy)
{
this.sys=sy;
 
}
 
 
 
 
public void position()
{
 
        try {
            //connexion();
            // Socket socket = new Socket("localhost", 45001);
            //InputStream in = socket.getInputStream();
            byte[] tab = new byte[20];
            //  int lu = in.read(tab);
 
             lu = sys.sock.entreeSocket.read(tab);
 
            a = new String(tab, 0, lu);
            System.out.println(a);
            sys.AffichePosition();
 
        } catch (IOException ex) {
            Logger.getLogger(CCommunication.class.getName()).log(Level.SEVERE, null, ex);
        }
 
}
 
    public void run() {
        while(true)
        {
        position();
        }
 
 
    }
}

Dans mon Main:

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
 
public class Main {
 
//  MaSocket sock = new MaSocket("172.16.157.11",45001);
 
  CSysteme sy;
  CCommunication com = new CCommunication(sy);
    /**
     * @param args the command line arguments
     */
    @SuppressWarnings("static-access")
    public Main() throws UnknownHostException{
 
     MyFenetre f = new MyFenetre();
     f.setSize(800,550);
     f.setVisible(true);      
     f.addKeyListener(f);
  //   com.position();
 
   Thread th ;
   th = new Thread(com);
    th.start();
     // sock.receptionFichier(); 
    // f.ReceptionPosition();
 
    }
Et donc voici l'erreur :

Exception in thread "Thread-3" java.lang.NullPointerException
at fenêtre.CCommunication.ReadPosition(CCommunication.java:38)
at fenêtre.CCommunication.run(CCommunication.java:84)
at java.lang.Thread.run(Thread.java:619)