[FIXED]my array loads up to 10 items and then gives a NullPointerException. Where did I go wrong?
in
Core Library Questions
•
1 year ago
**update** all the code below works perfectly. I found out that two out of many sound files were .wma format and not .wav..this caused processing to through an error because it did not match what was listed in the text files i am loading.
**
right now I have two arrays.
1. loads and stores a list of images
2. loads and stores a list of sounds
the images array works perfectly. loads all 26 images
the sounds array however loads at the most 10 and then throws a "NullPointerException". Ive checked my spelling, math, adding memory in preferences, everything. I can load any number of items as long as I dont go over 10.
even if i comment out the images load method and only load the sounds it fails.
im using minim 2.02
and processing 2.0b3
everything works and runs smoothly as long as I dont load more than 10 items into the sound array.
the sketch allows you to load 26 images and 26 sounds from a text file. when you click on the image, lets say of a monkey it will play the corresponding sound of the monkey. you can cycle through the images and sounds at the same time with two 'buttons' on the left and right of the image.
code:
import ddf.minim.*;
import ddf.minim.signals.*;//dont need yet
import ddf.minim.analysis.*;//dont need yet
import ddf.minim.effects.*;//dont need yet- //a variable to contain our array number
//set to zero because that is the default
//value of an array
int imgVar = 0;
//our main array var's
int i;//image
int j;//sound
//name our arrays
String[] imgUrls;
String[] sndUrls;
PImage[] images;
AudioSnippet[] sounds;
//minim
Minim minim;
void setup(){
size(700, 600);
background(0);
loadSnd();//load sounds
loadImg();//load images
}//setup
void draw(){
drawRecR();
drawRecL();
//make sure the image is refreshed and correct
image(images[i], 50, 0 );
}//draw
void mousePressed(){
//if the mouse is over left button
if (mouseX >= 0 && mouseX <= 50){
println("left button");
i--;
j--;
if ( i < 0){
//must subtract from array length to
//account for 0 being counted
i = images.length - 1;
j = sounds.length - 1;
}//if
}//ifML
//if the mouse is over the center image
else if (mouseX >= 51 && mouseX <= 649){
println("center button");
//play the sound
play();
}//ifMC
//if the mouse is over the right button
else if (mouseX >= 650 && mouseX <= 700){
println("right button");
i++;
j++;
if ( i >= (imgUrls.length)){
i = 0;
j = 0;
}//if
}//ifML
println(i);
println(j);
println(mouseX);
}//mouse pressed
//push list of images into array
void loadImg(){
imgUrls= loadStrings("listI.txt");
images = new PImage[imgUrls.length];
for(int i = 0; i<imgUrls.length;i++){
images[i] = loadImage(imgUrls[i]);
println(i);
}//fori
}//loadImg- //push list of sounds into array
void loadSnd(){
sndUrls= loadStrings("listS.txt");
sounds = new AudioSnippet[sndUrls.length];
for(int j = 0; j<sndUrls.length;j++){
minim = new Minim(this);
sounds[j] = minim.loadSnippet(sndUrls[j]); //it always says this is the NullPointerException
println(j);
}//fori
}//loadSnd
////draws rectanges to visualize pre and next buttons
void drawRecL(){
fill(50);
rect(0,0,50,600);
}//drawRec
void drawRecR(){
fill(25);
rect(650,0,50,600);
}//drawRec
//control when the sound file is played
void play(){
//if it is not playing then go ahead and play
if (!sounds[j].isPlaying()){
// makes sure the file plays from start
sounds[j].rewind();
sounds[j].play();
}//if
}//play- //control when the sound file is stoped
void close(){
sounds[j].close();
minim.stop();
//
super.stop();
}//stop
1