NullPointerException using minim

edited February 2017 in Library Questions

Hello,

I am trying to do something but I got an error. What I wanna do is to add a default sound to a custom class that will be played each time I click on it. So I created a new object :

Minim minim;
AudioPlayer globalsound;

void setup()
{
  size(1366,768);
  globalsound= minim.loadFile("sound.mp3");
}

void draw()
{
  //do things
}

class Mynewobject
{
  AudioPlayer defaultsound;

  Mynewobject()
  {
    this.defaultsound=globalsound;
    this.defaultsound.play();
  }
}

Can you please help me to find out what I'm doing wrong ? Thanks in advance !

Tagged:

Answers

  • To help people in the forum read and run your code and give feedback, please format your code -- edit your post, highight the code, and press CTRL+o.

  • Thank you, I have done this, I hope it will help !

  • Please edit your post and format your code. Select your code and press ctrl+o and make sure you have an empty line above and below your code.

    You need to call play() or loop() in setup for the file to be executed. On the side, if your class is going to control the AudioPlayer object, why not to include this AudioPlayer object in your class? Then you need to create an object of your class and you could call associated functions to interact with your AudioPlayer object, like I show below.

    Please note the code below was not tested but to demonstrate a concept.

    Check minim documentation: http://code.compartmental.net/tools/minim/
    http://code.compartmental.net/tools/minim/quickstart/
    http://code.compartmental.net/minim/javadoc/

    Kf

    Mynewobject myplayer;
    
    void setup(){
      size(400,400);
      myplayer = new Mynewobject("sound.mp3");
    }
    
    void draw(){};
    
    void mouseReleased(){
      myplayer.toggleStatus();
    }
    
    class Mynewobject{
    
    AudioPlayer defaultsound;
    
    Mynewobject(String fn){
      defaultsound=minim.loadFile(fn);
    }
    
    void playNow(){
      defaultsound.play();
    }
    
    
    void pauseNow(){
      defaultsound.pause();
    }
    
    void toggleStatus(){
    
     if(defaultsound.isPlaying()==true)
      pauseNow();
    else
      playNow();
    }
    }
    
  • edited February 2017

    Thank you for your answer ! I will try this. I'm sorry if I did short, I removed things on purpose to make the code more readable. My idea was to have a default sound for the superclass, wich I can change in the children classes, that's why I need to put a default one in a first time. Thanks again !

    I'm trying to make the default constructor put the sound, something like this :

    Mynewobject myplayer;
    
    void setup(){
      size(400,400);
      myplayer = new Mynewobject();
    }
    
    void draw(){};
    
    void mouseReleased(){
      myplayer.toggleStatus();
    }
    
    class Mynewobject{
    
    AudioPlayer defaultsound;
    
    Mynewobject(){
      defaultsound=minim.loadFile("sound.mp3");
    }
    
    void playNow(){
      defaultsound.play();
    }
    
    
    void pauseNow(){
      defaultsound.pause();
    }
    
    void toggleStatus(){
    
     if(defaultsound.isPlaying()==true)
      pauseNow();
    else
      playNow();
    }
    }
    

    I have a NullPointerException, but still don't understand why...

  • Answer ✓

    You need a global variable of type Minim to use inside your class. This global variable should be initialised inside setup before trying to use Minim.

    I have also altered the constructor so that you can have separate objects for each sound file.

    import ddf.minim.*;
    import ddf.minim.analysis.*;
    import ddf.minim.effects.*;
    import ddf.minim.signals.*;
    import ddf.minim.spi.*;
    import ddf.minim.ugens.*;
    
    Mynewobject myplayer;
    Minim minim;
    
    void setup() {
      size(400, 400);
      // Initialise minim
      minim = new Minim(this);
      myplayer = new Mynewobject("sound.mp3");
    }
    
    void draw() {
    }
    
    void mouseReleased() {
      myplayer.toggleStatus();
    }
    
    class Mynewobject {
    
      AudioPlayer defaultsound;
    
      // Constructor modified
      Mynewobject(String soundFile) {
        defaultsound = minim.loadFile(soundFile);
      }
    
      void playNow() {
        defaultsound.play();
      }
    
      void pauseNow() {
        defaultsound.pause();
      }
    
      void toggleStatus() {
    
        if (defaultsound.isPlaying()==true)
          pauseNow();
        else
          playNow();
      }
    }
    
  • edited February 2017

    Thanks again ! I still don't understand a thing. Why does this is allowed (I assume, I haven't tried yet) :

    Mynewobject(String soundFile)
    {
      defaultsound = minim.loadFile(soundFile);
    }
    

    While this :

    Mynewobject()
    {
      String soundFile="pathtofile.mp3";
      defaultsound = minim.loadFile(soundFile);
    }
    

    is not allowed and will give me errors (for sure) ?

    edit : I tried both methods in a fresh new file. They are both working, so I made a mistake elsewhere in my code... I'll find it, thanks again !!! :)

  • That is NOT the problem that was an enhancement. The problem was that you missed this from setup

    minim = new Minim(this);

  • edited February 2017

    Yes ok, sorry, I didn't saw your last answer. I found the problem :

    I put the line :

    Mynewobject myplayer = new Mynewobject();

    in another .pde file (but in the same directory) By doing this, I got the same error ! I hope it will help people who have the same issue !

    edit : just forget what I said, I'm doing only bad things. I think that

    Mynewobject myplayer = new Mynewobject();

    is not allowed, I have to split it in many parts

Sign In or Register to comment.