# Why doesn't this work?

**URL:** https://discourse.processing.org/t/why-doesnt-this-work/22097
**Category:** Beginners
**Created:** [June 23, 2020, 2:08pm UTC](https://discourse.processing.org/t/why-doesnt-this-work/22097 "2020-06-23T14:08:16Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![mirthe24](https://avatars.discourse-cdn.com/v4/letter/m/aca169/32.png) [@mirthe24](https://discourse.processing.org/u/mirthe24)
#### Post date: [June 23, 2020, 2:08pm UTC](https://discourse.processing.org/t/why-doesnt-this-work/22097/1 "2020-06-23T14:08:16Z")

</div>

```auto
import processing.serial.*;
import processing.sound.*;

SoundFile file;
Serial myPort; // Create object from Serial class
String val; // Data received from the serial port
//String antwoord = "A";
void setup()
{
  size(300,300);
  // I know that the first port in the serial list on my mac
  // is Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600);
}

void draw()
{
  if ( myPort.available() > 0) 
  { // If data is available,
  val = trim( myPort.readStringUntil(ENTER) );
       // read it and store it in val
  } 
//println(val); //print it out in the console
 
if ("A".equals(val) == true && file.isPlaying() == false) {
 
file = new SoundFile(this,"Promise.mp3");
file.play();
file.amp(0.2);}

else{
 ellipse(40,40,40,40);
}
}

```

This is my code, but i get a nullpointer exception at this line:

```auto
if ("A".equals(val) == true && file.isPlaying() == false) {

```

I just want to let the file below it play, when it isn’t already playing.

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [June 23, 2020, 4:27pm UTC](https://discourse.processing.org/t/why-doesnt-this-work/22097/2 "2020-06-23T16:27:24Z")

</div>

you might want to check that `val` does in fact have a value with a simple `if(val!=null)`,  
reason is if your fist if statement fails, then val is null as you havent initialised it with a value.

Also you arent initializing file until after the if statement

you are checking to see if the file is playing before telling it what file to play.

```auto

check for file play---> if ("A".equals(val) == true && file.isPlaying() == false) {
 
file is null until you call file = new SoundFile(this,"Promise.mp3");
then you ask it to play---> file.play();
                             file.amp(0.2);}

```
