# Why I cannot run code NullPointerException

**URL:** <https://discourse.processing.org/t/why-i-cannot-run-code-nullpointerexception/10378>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [April 17, 2019, 7:19am UTC](https://discourse.processing.org/t/why-i-cannot-run-code-nullpointerexception/10378 "2019-04-17T07:19:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![warayutsitha2](https://avatars.discourse-cdn.com/v4/letter/w/82dd89/32.png) [@warayutsitha2](https://discourse.processing.org/u/warayutsitha2)\
**Post date:** [April 17, 2019, 7:19am UTC](https://discourse.processing.org/t/why-i-cannot-run-code-nullpointerexception/10378/1 "2019-04-17T07:19:16Z")

</div>

```auto
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);

 }

```

 ![2019-04-17_14-14-14](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/1e0096a6fc5ab0c7e9491d199eec3fea363d3785.png)

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [April 17, 2019, 9:44am UTC](https://discourse.processing.org/t/why-i-cannot-run-code-nullpointerexception/10378/2 "2019-04-17T09:44:25Z")

</div>

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

```auto
</> code tag

```

and you use a very old processing 2.2.1 ( any reason for this?)  
[https://processing.org/download/](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.

---

<div class="post-metadata">

**Author:** ![warayutsitha2](https://avatars.discourse-cdn.com/v4/letter/w/82dd89/32.png) [@warayutsitha2](https://discourse.processing.org/u/warayutsitha2)\
**Post date:** [April 17, 2019, 11:06am UTC](https://discourse.processing.org/t/why-i-cannot-run-code-nullpointerexception/10378/3 "2019-04-17T11:06:59Z")

</div>

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

 ![Inkedjavaw_2019-04-17_17-55-18_LI](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/0/077bb73f2bf4c41f8ddd9383b553d802468228e6.jpeg)

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [April 17, 2019, 12:24pm UTC](https://discourse.processing.org/t/why-i-cannot-run-code-nullpointerexception/10378/4 "2019-04-17T12:24:50Z")

</div>

not understand your concept ( or question ),

- but inside serialEvent
- 
  - i doubt the key detection

- 
  - also think there never should be a loop / noLoop

---

<div class="post-metadata">

**Author:** ![warayutsitha2](https://avatars.discourse-cdn.com/v4/letter/w/82dd89/32.png) [@warayutsitha2](https://discourse.processing.org/u/warayutsitha2)\
**Post date:** [April 17, 2019, 12:48pm UTC](https://discourse.processing.org/t/why-i-cannot-run-code-nullpointerexception/10378/5 "2019-04-17T12:48:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [April 17, 2019, 12:56pm UTC](https://discourse.processing.org/t/why-i-cannot-run-code-nullpointerexception/10378/6 "2019-04-17T12:56:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![warayutsitha2](https://avatars.discourse-cdn.com/v4/letter/w/82dd89/32.png) [@warayutsitha2](https://discourse.processing.org/u/warayutsitha2)\
**Post date:** [April 17, 2019, 1:04pm UTC](https://discourse.processing.org/t/why-i-cannot-run-code-nullpointerexception/10378/7 "2019-04-17T13:04:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![warayutsitha2](https://avatars.discourse-cdn.com/v4/letter/w/82dd89/32.png) [@warayutsitha2](https://discourse.processing.org/u/warayutsitha2)\
**Post date:** [April 17, 2019, 1:06pm UTC](https://discourse.processing.org/t/why-i-cannot-run-code-nullpointerexception/10378/8 "2019-04-17T13:06:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [April 17, 2019, 3:20pm UTC](https://discourse.processing.org/t/why-i-cannot-run-code-nullpointerexception/10378/9 "2019-04-17T15:20:29Z")

</div>

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
