Minim NullPointerException after multiple executions

edited December 2015 in Library Questions

The following code gives me a NullPointerException

// file: Lily.pde

import ddf.minim.*;
AudioPlayer player;
Minim minim; //audio context

// List of animals
StringList animalList = new StringList(
      "alligator", "bear", "cat", "dog", "elephant",
      "frog", "goat", "horse", "jay",
      "koala", "lion", "monkey", "newt", "owl",
      "pig", "raccoon", "squirrel", "turkey", "vulture",
      "whale", "yak", "zebra");

// animal image, sound and name matricies 
PImage[] images = new PImage[animalList.size()];
String[] animalSound = new String[animalList.size()]; 
String[] animalName = new String[animalList.size()];

String aList = "";
String aName = "";
PImage lily;
String imgName;
String sound;
int col;

void setup() {
  size(1024, 960);

  // Create and load image & sound matricies
  // ---------------------------------------
  int i = 0;
  for (String animal : animalList) {
    imgName = "data/" + animal + ".jpg";
    images[i] = requestImage(imgName);
    sound = "data/" + animal + ".mp3";
    animalSound[i] = sound;
    aName = "data/say-" + animal + ".mp3";
    animalName[i] = aName;
    aList += animal.charAt(0);
    i ++;

  minim = new Minim(this);

  // Load start image and sound
  // --------------------------


void draw() {


void keyPressed(){
 if(player.isPlaying()) {

  col = color(random(255), random(255),random(255));
  int key1 = aList.indexOf(key);
  if (key1 == -1) {
    text(Character.toUpperCase(key), width/2, height/2);
  return; }




  char letter = aList.charAt(key1);
  showLetter(letter, animalList.get(key1));

void playSound(int k) {
  player = minim.loadFile(animalSound[k]);;                                              <<- null pointer occurs here or below

void playName(int k) {
 player = minim.loadFile(animalName[k]);;                                                 <<- null pointer also occurs here

void showLetter(char letter, String animal) {
  col = color(random(255), random(255),random(255));
  letter = Character.toUpperCase(letter);
  text(letter, 50, 110);
  text(animal, width/2 - (animal.length()/2), height -75);
  text(letter, width-100, height-50);

void showImage(int k) {
 image(images[k], width/2, height/2, 700,600);

void showLily() {
  col = color(random(255), random(255),random(255));
  imgName = ("data/Lily1.jpg");
  lily = loadImage(imgName);
  image(lily, width/2, height/2);
  showLetter('L', "Lily");

void stop() {

I get the NullPointerException at the places indicated above. I have tried placing

if(player.isPlaying()) {

at the top of both the playSound() and playName() functions but neither has the effect I expect which is to let the next sound proceed. NullExceptionPointers do not occur after every new keystroke but as I increase the speed after about 5 or 6 keys I will get the exception. This application is for my 18 month old grand-daughter so of course she will "pound" the keyboard. Anyone have any ideas. I am using Processing 3.01 with the Minim imbeded library.


  • Answer ✓
    • draw() is the right place to draw!
    • Other callbacks like keyPressed() should be kept to a minimum code.
    • Mostly to just flag which key or mouse button had been hit.
  • ****GoToLoop Thank you for the quick response. Since I am a novice I'm going to think a little bit before responding with a question/comment. Ron

  • edited December 2015

    Declare a global variable: boolean keyHit;
    Inside keyPressed(): keyHit = true;
    Inside draw(), have this if () {} block:

    if (keyHit) {
      keyHit = false;
      // Place the rest of the code which was in keyPressed() below:
  • GoToLoop I've placed all the former keyPressed() code into draw(). keyPressed() now only contains the loop() function. Draw ends with noLoop() function. I still have the same NullPointerException, but also get a new error:

    Couldn't open the line: line with format PCM_SIGNED 44100.0 Hz, 
    16 bit,    stereo, 4 bytes/frame, little-endian not supported.

    That is reported here


    There a few other issues but I think I can figure them out. Thanks for

  • GoToLoop, Looks like I missed your 12:37 edit. I'll strip the loop() and noLoop() functions and use your keyHit code. Thanks again, Ron

  • Answer ✓

    You can also issue noLoop() within setup().
    Then issue redraw() within keyPressed() after keyHit = true;

  • GoToLoop (I love this handle); All your suggestions have really helped me and have improved my code. However, I still get the same PCM_SIGNED error after so many executions. I am using Minim since it was included in Processing 3.01 and because Sound gives me an error which is also documented on the web. So my questions is if you have a preferred sound library that I might try? Thanks again.

  • I don't have much experience w/ Minim much less Sound libraries.
    I was just trying to help w/ the basic logic of your sketch rather than deal w/ the library.
    Nonetheless, you could post your most current attempt so others can help ya out too.

  • You definitely improved the logic!

Sign In or Register to comment.