Why I cannot run code NullPointerException

#1
import processing.serial.*;
Serial myPort;
String inBuffer;
PImage img;
String[] txt = split(inBuffer,' ');

void setup(){
   
  myPort = new Serial(this, "COM9", 115200);
  myPort.bufferUntil('\n');
}

void draw(){
   **byte[] sampleBytes = new byte[txt.length];** //this line NullPointerException
  for ( int i=0;i<txt.length;i++){
  sampleBytes[i]=byte(unhex(txt[i]));
  saveBytes("image1.jpg", sampleBytes);
  
}
}

void keyPressed()
{
  if ((key == 'b') | (key == 'B'))
  {
    myPort.write(98);
 }
}

void serialEvent(Serial p)
{
  inBuffer = p.readString();  
  print(inBuffer);

 }

0 Likes

#2

besides that you posted your code as text
and not inside the

</> code tag

and you use a very old processing 2.2.1 ( any reason for this?)
https://processing.org/download/


i think your problem is that you try define arrays
inside of draw or serial event
( usually it should be defined global and filled in setup )
and
you make that depending on another array variable length β€œtxt”
what is not filled?


and again the question has nothing to with arduino.

0 Likes

#3

Hello, I need some help for normally my data in big loop is one data and when i press β€˜B’ it will change to another data and when out of small loop i want go back to big loop() what can i do ?
redraw() ? noLopp() ?

0 Likes

#4

not understand your concept ( or question ),

  • but inside serialEvent
    • i doubt the key detection
    • also think there never should be a loop / noLoop
0 Likes

#5

Normally, my output in Arduino sent some data is β€œ00000000000000000000000000” (example) and When i sent character ’ B ’ to serial port in Arduino it’s will sent β€œ111111111111111111” to serial port

and in my Proxessing in Serial Event(); i use keypress(); to show β€œ1111111111111” data and when it has sent already sent β€œ000000000000” before.

0 Likes

#6

oh, and then why you split that string on β€œ,” and again on " " ?

anyhow you will see when other data arrive,
questionable to use the key == β€˜b’ here,
because if it is still available there, you not know how old it is.

0 Likes

#7

Normally, In y code ardiuno is sent β€œSERIAL:ID15,ok,0:0:0,0.000000,0.000000,12.80,2134.87,-6754.32,0.00,0.00,0.00” to serial monitor
and in my code Arduino when i press β€˜B’ it will sent hex data from JPEG CAMERA MODULE to serial port like"FF D8 FF E0 00 10 4A 46 49 46 00 01 01 01 …(more)…" so in my Processing I have to split for use Savebyte(); to image when i press’B’ i want to receive camera data and SaveByte(); to image and when it save already i want to go back to show "β€œSERIAL:ID15,ok,0:0:0,0.000000,0.000000,12.80,2134.87,-6754.32,0.00,0.00,0.00” like before.

0 Likes

#8

i split use β€œ,” for ID15,ok,0:0:0,0.000000,0.000000,12.80,2134.87,-6754.32,0.00,0.00,0.00
and when i press β€˜B’ i have to use split " " for Camera hex data to Savebytes(); in Processing.

0 Likes

#9

Try to avoid working with multiple issues in the same post. Your initial post is about NPE. Is that issue solved? If you have another issue, create a separate post if not related to your original question with your updated code. Also, it is better you provide code instead of screenshots in most cases.

Kf

1 Like