Conditioning the sketch on finding the COM port trouble

Thanks a lot friend. Yes the ardu goes on COM27 when connected.

Its my first time playing with “try” and I tried

import processing.serial.*;
Serial myPort;

int lf = 10; 

void setup() {
  size(1900, 950, JAVA2D);
  printArray(Serial.list());

  try {
    Serial myPort = new Serial(this, "COM27", 115200);
    myPort.clear();
  } catch (Exception e) { 
    e.printStackTrace();
    delay (3000);
    println("NoPORT");
    //exit();
  }
}


void draw() { 
  background(0);


    if (myPort.available() > 0) {
      String IncomingSTR = myPort.readStringUntil(lf);
      println (IncomingSTR);
    }
    
}

I am getting a null pointer when the Ardu is connected and when is not.

When is not I do get the printout println(“NoPORT”); but not the one above.

I would want not to get a null pointer and give the user a chance to connect the Ardu.

but the first problem, first:
When the Ardu is connected, I get a null pointer and it gets stuck on
if (myPort.available() > 0) { line

The situation is quite messy because of windows. I found in device manager several “Ghosts” comm ports that get picked occasionally by processing as real ports…

ghost_Ports

To be concise: I would love to write a condition in Setup that will connect the sketch to the only active port in windows. (This time Com27) and would ignore the rest.

Sometimes I get in the (Serial.list()) ports that are greyed out but still indexed as [0] and one… halting the sketch. Then I delete them in device manager and re-boot and then I pick the [0] in the sketch and it works fine…

Any advice, thank you,
Mitch

Thanks
Mitch