Why I cannot run code NullPointerException

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

void setup(){
  myPort = new Serial(this, "COM9", 115200);

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

void keyPressed()
  if ((key == 'b') | (key == 'B'))

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


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?)

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 )
you make that depending on another array variable length β€œtxt”
what is not filled?

and again the question has nothing to with arduino.

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() ?

not understand your concept ( or question ),

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

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.

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.

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.

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.

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.


1 Like