Trying to make 2 strings out of one Serial transmission

I made a GUI for an Arduino robot. Every 500 mS I am getting a serial communication and it works well.

Now I have two data strings. One pertains to the Motor Currents and it always starts with M.

The other provides other general info on the robot and never starts with M.

So I thought I can create 2 strings and show them on the screen at different locations.

if (BTPort.available() > 0) {
  IncomingStr = BTPort.readStringUntil(lf); 
 }
  
  First = IncomingStr.charAt(0);
  if (First == 'M')
  Incoming_Currents = IncomingStr;
  
  else
  Incoming_Info = IncomingStr;

It did not work, I get a null pointer…

Wonder why

Thank you
Mitch

Hello,

Check for a null character.
You can initialize strings also; be sure to test code as well for valid data. Prefix each with a character.
See example below.

String Initialization in Java

I also have an example here that may be of interest:

:)

Thanks a lot , I could not use “match” but I got it to work.

 while (BTPort.available() > 0) {
    IncomingStr = BTPort.readStringUntil(lf);
    if (IncomingStr != null) {
      First = IncomingStr.charAt(0);
      println(IncomingStr);
      println(First);
      
  if (First == 'M')  
   Incoming_Currents = IncomingStr; // incoming current string from teensy
   
   
   
   else 
   Incoming_Info= IncomingStr;  //  incoming current string from teensy
    }
  }
    

The essential part was if (IncomingStr != null).
I was under the impression that the string stays at the last value till the next transmission, but it goes to Null instead.
During null, my if else functions did not work.

Thanks
Mitch

2 Likes