We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am creating a sketch which is music visualization.The code is provided below and the source of the error is highlighted in red. How do I resolve the NullPointerException error?
/* OpenProcessing Tweak of *@*http://www.openprocessing.org/sketch/2271*@* */
/* !do not delete the line above, required for linking your tweak if you upload again */
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.*;
Minim minim;
AudioPlayer song;
BeatDetect beat;
BeatListener bl;
ArrayList balls = new ArrayList();
void setup()
{
size(640, 480);
// always start Minim before you do anything with it
minim = new Minim(this);
frameRate( 30 );
smooth();
song = minim.loadFile("droost.mp3", 2048);
beat = new BeatDetect(song.bufferSize(), song.sampleRate());
beat.setSensitivity(0);
bl = new BeatListener(beat, song);
song.play();
noStroke();
}
void draw()
{
fill( 0, 0, 0, 45 );
rect(0, 0, width, height);
// use the mix buffer to draw the waveforms.
// because these are MONO files, we could have used the left or right buffers and got the same data
boolean kick = beat.isKick();
boolean hat = beat.isHat();
boolean snare = beat.isSnare();
if( beat.isRange( 5, 15, 2 ) )
{
color col = color( random(255), random(255), random(255) );
for( int j = 0; j < abs(song.mix.level() * 50); j++ )
{
float y = random( height );
float x = random( width );
for (int i = 0; i < abs(song.mix.level()*100); i++)
{
balls.add( new Ball( x, y, song.mix.get(0)*70, col ) );
}
}
}
for( int i = 0; i < balls.size(); i++ )
{
Ball b = (Ball)balls.get(i);
b.update();
if( !b.alive )
{
balls.remove( b );
i--;
}
}
}
void stop()
{
// always close Minim audio classes when you are done with them
song.close();
minim.stop();
super.stop();
}
public class Ball
{
PVector loc = new PVector();
PVector speed = new PVector( random( -2, 2 ), random( -2, 2 ) );
color col;
boolean alive = true;
int age = 0;
public Ball( float x, float y, float mag, color col )
{
loc.x = x;
loc.y = y;
speed.normalize();
speed.mult( mag );
this.col = col;
}
public void update()
{
age += 3;
speed.y += .1;
loc.add( speed );
if( loc.y > height || age >= 255 )
alive = false;
fill( red(col), blue(col), green(col), 255 - age );
ellipse( loc.x, loc.y, 5, 5 );
}
}
class BeatListener implements AudioListener
{
private BeatDetect beat;
private AudioPlayer source;
BeatListener(BeatDetect beat, AudioPlayer source)
{
this.source = source;
this.source.addListener(this);
this.beat = beat;
}
void samples(float[] samps)
{
beat.detect(source.mix);
}
void samples(float[] sampsL, float[] sampsR)
{
beat.detect(source.mix);
}
}
the highlight line is "song = minim.loadFile("droost.mp3", 2048);"
Answers
I have moved this to Questions about Code because it is more appropriate.
I can't see anything highlighted in red - could you give the line number for the offending statement in the above code.
For what it is worth, there is an article on this kind of error: Why do I get a NullPointerException?
When you get an error message some text appears in the PDE output window - the bottom pane with the black background. It is always useful to include this in your post . When I ran the sketch I got
The error was caused in the minim library and the problem is that the sketch can't find the file "droost.mp3" when I ran the code with my own mp3 it worked fine.
The file must be in the sketch folder (the one with the pde file) OR a folder called data inside the sketch folder.
BTW forget to say it looked great - =D>