Loading...
Logo
Processing Forum
I have an application that usess multiple serial ports. When I run my application it automaticaly connects to all serial ports that are read from file (well their names are). I simply store the opened ports into an array and that is it. If all ports are connected everything is good. But when I try to send something to a port that is not connected I get NullPointerException. I tryed catching the exception, checking if serial port is null but nothing works. Is there any way to check if the port is actualy working before I try sending something and getting an exception that I can't catch?

Replies(5)

You can always catch an exception, unless Processing itself is catching it and just displaying the result. How do you try to catch it?
As you would cath any exception.

Copy code
  1. try
  2. {
  3.       porti.get(selectedSobaIndex).write("IN");
  4. }
  5. catch (NullPointerException e)
  6. {
  7.       println("blabla");
  8. }
But I allways NullPointerException from processing - and my catch never gets triggered.
I see in Serial.java that all exceptions are caught and dumped or displayed. That's the classical Processing way (avoiding people to handle exceptions as it is a confusing part of newbies) but it can be annoying for more advanced uses. Now, something like write() is only two lines of Java, so you can rewrite it and handle the exception yourself.
That is one solution but write() isn't the only one. It is the most important one. I like that exceptions are caught by processing by default because it won't crash my application if I forget to implement my own try/catch. Main problem with this is that I can't notify the user when the exception gets triggered. I want a standalone application and all I want to do with this exception is to let the user know that something is wrong.

EDIT: I was just trying to override the write method and I am having problems with constructors from Serial library.
Im resurrecting this thread because I am having the exact same issue and was wondering if maybe now here is a workaround.
 
my sketch interacts with two com ports on my pc.  I need to let the user know that there is a USB cable unplugged in a user friendly way rather than having them deal with the RED exception text at the bottom of the processing window.
Ultimately I want to be able to make an executable out of this so Id rather handle the error "inhouse" and have the sketch notify them of the unplugged cable and wait for them to connect it or even just tell them to connect it and start the application over again.
 
Any Idea how to do this????