Ilias Kyriazis
Ilias Kyriazis

Reputation: 81

NullPointerException with Processing and audio input

I am working on a Processing sketch, where I am getting input from an audio file, whose main frequency I am mapping to the movement of three animated "snakes". The code works sometimes fine, and sometimes not (NullPointerException)! I guess it has to do with some detail, that I am missing/don't get, but I am getting confused, since my code doesn't break every time!! If you could check it and figure out the problem, I would be grateful! Here is the main sketch:

import beads.*;

// sound variables
AudioContext ac;
Gain g;
PowerSpectrum ps;
Frequency f;
String sourceFile;
SamplePlayer player;
float inputFrequency; 
float mappedFrequency;
float prevFrequency;

// visual variables
ShapeA shA1;
ShapeA shA2;
ShapeA shA3;
boolean shA1pressed, shA2pressed, shA3pressed;

void setup() {
  size(900, 600);
  frameRate(30); // doesn't affect sound
  inputFrequency = 0;
  mappedFrequency = 0;
  prevFrequency = 0;

  // get sound source
  ac = new AudioContext();
  sourceFile = sketchPath("") + "data/rite1.mp3";  
  try {
    player = new SamplePlayer(ac, new Sample(sourceFile));
  }
  catch(Exception e)
  {
    println("Exception while attempting to load sample!");
    e.printStackTrace();
    exit();
  }
  g = new Gain(ac, 2, 0.3);
  g.addInput(player);
  ac.out.addInput(g);

  /* prerequisites for getting main frequency */
  ShortFrameSegmenter sfs = new ShortFrameSegmenter(ac);
  sfs.addInput(ac.out);
  FFT fft = new FFT();
  sfs.addListener(fft);
  ps = new PowerSpectrum(); 
  fft.addListener(ps);
  f = new Frequency(44100.0f);
  ps.addListener(f);
  ac.out.addDependent(sfs); 
  /* end of prerequistes */

  ac.start();

  smooth();
  noStroke();
  //strokeWeight(2); // affecting the size of a point
  shA1 = new ShapeA(3); // random factor affecting angle
  shA2 = new ShapeA(6);
  shA3 = new ShapeA(8);
  shA1pressed = false;
  shA2pressed = false;
  shA3pressed = false;
}

void draw() {
  //background(255);
  fill(255,10);
  rect(0-10,0-10,width+20,height+20);

  text(" Input Frequency: ", -100, -100); // for some reason, needed!!
  inputFrequency = f.getFeatures(); // ** THIS IS THE MAIN FREQUENCY **
  mappedFrequency = map(inputFrequency,20,2000,0,20); // computational power!

  if (shA1pressed) {
    shA1.angle();
    shA1.display(shA1.angle);
  }  
  if (shA2pressed) {
    shA2.angle();
    shA2.display(shA2.angle);
  }
  if (shA3pressed) {
    shA3.angle();
    shA3.display(shA3.angle);
  }

  prevFrequency = inputFrequency;
}

void keyPressed() {
  if (key == '1') {
    if (shA1pressed == false) {
      shA1pressed = true;
    } else {
      shA1pressed = false;
    }
  }
  if (key == '2') {
    if (shA2pressed == false) {
      shA2pressed = true;
    } else {
      shA2pressed = false;
    }
  }
  if (key == '3') {
    if (shA3pressed == false) {
      shA3pressed = true;
    } else {
      shA3pressed = false;
    }
  }
}

And here is the ShapeA class:

class ShapeA {
 float angleCount = 7;
 float stepSize = 3.3; 
 int i = 0;
 float angle = radians(270);
 float r;

 ArrayList<PVector> points = new ArrayList<PVector>();

 ShapeA(float _r) {
   // set position of first point
   points.add(new PVector(random(width), random(height)));
   r = _r;
 }

 float angle() {
   if (prevFrequency <= inputFrequency) {
     angle = angle - radians(mappedFrequency*random(r)); 
   } else {
     angle = angle + radians(mappedFrequency*random(r));
   }
   return angle;
 }

 void display(float _angle) {
   PVector point = points.get(i);
   fill(0);
   ellipse(point.x, point.y, 3, 3);

   // add a new point
  PVector prevPoint = points.get(points.size()-1);
  float x = prevPoint.x + cos(_angle) * stepSize;
  float y = prevPoint.y + sin(_angle) * stepSize;
  points.add(new PVector(x, y));

  // check if shape hits display borders
  float a = (floor(random(-angleCount, angleCount)) + 0.5) * 90.0/angleCount;
  if (y <= 0) {
   angle = a+90;
  } 
  else if (x >= width) {
   angle = a+180;
  }
  else if (y >= height) {
   angle = a-90;
  }
  else if (x <= 0) {
   angle = a;
  }
  i++;
 }

}

When I am getting an error message, this is a NullPointerException message highlighting this line (in the main sketch):

inputFrequency = f.getFeatures(); // ** THIS IS THE MAIN FREQUENCY **

Additionally, I have also noticed, that by taking out the following line

text(" Input Frequency: ", -100, -100);

I also get the same type of message, although the line is of no use!

Could someone explain me, why are these "things" happening??

Thanks in advance, - Ilias

Upvotes: 2

Views: 941

Answers (1)

Kevin Workman
Kevin Workman

Reputation: 42174

In the future, you should try to post the full text of any errors you get, as well as links to the libraries you're using.

But I have a theory:

The Frequency class has a function called getFeatures() which returns a Float.

Note that capital F in that Float. It's an Object, not a primitive lower-case float. That means it can be null.

Then when you convert that to a lower-case float (through unboxing), you get a NullPointerException because it doesn't know how to convert null to a float.

I have no idea why that function is returning null; you'll have to check the documentation and do some debugging for that. But you either have to track the reason down and prevent it, or you have to add a null check that only enters your code if the value is non-null. Something like this:

Float returnedFrequency = f.getFeatures();
if(returnedFrequency != null){
   inputFrequency = returnedFrequency;
   //rest of your code

As for why you need the text() function, my guess is that it's merely a coincidence. I guess maybe it's causing enough of a delay for whatever needs to happen in the Frequency class, but that delay should only be a couple milliseconds, so I really doubt that's the case. Anyway, it becomes a moot point if you track down the cause of the null or add a null check.

By the way, you've posted a bunch of extra code here- your problem has nothing to do with the keyboard, so all of that code is just cluttering up your question. Next time, try to create an MCVE- just a few lines that we can run to get the same error, not your whole sketch. Good luck.

Upvotes: 1

Related Questions