I am using Processing together with an Arduino Yun. Most of the time it works fine, each even second I make measurements with the Arduino and each even minute i send a request from Processing to the Arduino to send me the mean of the last measurements. But sometimes it happens that Processing hangs.I get a "myClient" which is not "null", but a timeout! But when I make a "myClient. write" command after a "if (myClient != null)" I get directly a "NullPointerException"!
Here the important part of my code with the error message:
Main Program
297 // Get Client Anfang!
298 if((second() == 0) && (minute()%intervallMessung == 0) && !connected)
299 {
300 clientTime = true;
301 timeOut = 0;
302 }
303
304 if (clientTime) getClient();
305
306 if (connected && !sent) sendClient();
Method getClient
319 public void getClient()
320 {
321 fehler = false;
322 if (ausdruck)
323 {
324 print(nf(hour(),2) + ":" + nf(minute(),2) + ":" + nf(second(),2));
325 println(" Client Anfrage");
326 }
327 myClient = new Client(this, "192.168.2.162", 5555);
328 if (myClient != null)
329 {
330 if (ausdruck)
331 {
332 print(nf(hour(),2) + ":" + nf(minute(),2) + ":" + nf(second(),2));
333 println(" Client verbunden" + myClient);
334 }
335 connected = true;
336 clientTime = false;
337 } else {
338 if (ausdruck)
339 {
340 print(nf(hour(),2) + ":" + nf(minute(),2) + ":" + nf(second(),2));
341 println(" Client nicht verbunden : " + myClient);
342 }
343 fehler = true;
344 clientTime = false;
345 }
346 }
Output getClient
15:02:00 Client Anfrage
java.net.ConnectException: Operation timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.(Socket.java:425)
at java.net.Socket.(Socket.java:208)
at processing.net.Client.(Unknown Source)
at Wetterstation.getClient(Wetterstation.java:327)
at Wetterstation.draw(Wetterstation.java:304)
at processing.core.PApplet.handleDraw(PApplet.java:2306)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:243)
at processing.core.PApplet.run(PApplet.java:2177)
at java.lang.Thread.run(Thread.java:744)
15:03:15 Client verbunden : processing.net.Client@154c40f5
Obviously everything OK because "myClient != null"
But I got an timeout message!
How can I than detect an ConnectException? Normally with the "try" "Catch" method! But that doesn't work!
So, because the program doesn't know anything about an exception the method sendClient is started.
method sendClient
349 public void sendClient()
350 {
351 if (timeOut < 60)
352 {
353 if (ausdruck)
354 {
355 print(nf(hour(),2) + ":" + nf(minute(),2) + ":" + nf(second(),2));
356 println(" Vor myClient.write" + myClient);
357 }
358 if ( myClient != null)
359 {
360 myClient.write("\n");
361 if (ausdruck)
362 {
363 print(nf(hour(),2) + ":" + nf(minute(),2) + ":" + nf(second(),2));
364 println(" Nach myClient.write");
365 }
366 gesendet = true;
367 } else {
368 println("NullPointer um " + nf(hour(),2) + ":" + nf(minute(),2) + ":" + nf(second(),2));
369 println("Client nicht verbunden");
370 timeOut = second();
371 }
372 } else
373 {
374 fehler = true;
375 gesendet = false;
376 }
377 }
Obviously nothing OK
myClient = null!!!! although the if statement was true!
Why is myClient null after after an if (myClient != null) statement ?
15:03:15 Vor myClient.write : processing.net.Client@154c40f5
java.lang.NullPointerException
at processing.net.Client.write(Unknown Source)
at processing.net.Client.write(Unknown Source)
at Wetterstation.sendClient(Wetterstation.java:360)
at Wetterstation.draw(Wetterstation.java:306)
at processing.core.PApplet.handleDraw(PApplet.java:2306)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:243)
at processing.core.PApplet.run(PApplet.java:2177)
at java.lang.Thread.run(Thread.java:744)
15:03:15 Nach myClient.write : processing.net.Client@154c40f5
Why is it not possible to detect exceptions with the "try" and "catch" method? In Java is that the normal method.