Why doesn't this work?

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:

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

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

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.


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);}