about SinOsc array: difficulty in stop the sound (sounds stupid!)

edited November 2016 in Library Questions

Hi, I'm writing a coding for assignment, just one last step: stop the sound. I use an array to store the sound, but when I call it to stop ( sin[y].stop(); ), it would not! It keeps saying Null Pointer Exception. I know the reason that it is null before I selected a square, but it does not work either when I use specific value (sin[0].stop(); etc.).

Here's the full coding.

import processing.sound.*;
SinOsc [] sin;

int t=0;       
int spacing = 10;
int rectSize = 40;
boolean [][] selected;
int [] notes = {880, 990, 1110, 1320, 1480};

void setup()
{
  size(810,500);
  background(129,199,212);

  selected = new boolean[16][5];
  for (int i = 0; i < selected.length; i++)
  {
    for(int j = 0; j < selected[i].length; j++) 
    {
    selected[i][j] = false;
    }
  }
  sin = new SinOsc[5];
}
void draw()
{
  if(frameCount%20==1)
  {
    t=(t+1)%16;

    sin[0].stop();    //the question is here!~

    for (int y=0; y < 5; y++)
      {
        if(selected[t][y])
        {
        sin[y] = new SinOsc(this);
        sin[y].freq(notes[y]);
        sin[y].play();
        }
      }
  }

 //draw rectangles
  for(int i = 0; i < 16; i++)
  {
    for (int j = 0; j < 5; j++)
    {
    int a = spacing*(i+1)+rectSize*i;
    int b = spacing*(j+1)+rectSize*j;
    if(selected[i][j]) fill(150,150,255);               //selected = blue
    else fill(254,223,225);                             //unselected = pink
    if(i==t) fill (208,90,110);                         //red bar according to time
    noStroke();
    rect(a, b, rectSize, rectSize);
    }
  }

  //interaction buttons
  fill(254,223,225);
  rect(270, 2*height/3,80,80);
  rect(375, 2*height/3,80,80);
  rect(480, 2*height/3,80,80);

  fill(208,90,110);
  rect(285, 2*height/3+15, 50, 50);
  triangle(390,2*height/3+10,390, 2*height/3+70, 440,2*height/3+40);
  translate(105,0);
  triangle(390,2*height/3+40, 440, 2*height/3+10, 440, 2*height/3+70);
  rect(380, 2*height/3+10,10,60);
}

void mousePressed()
{
  int redX = mouseX/(spacing+rectSize);
  int redY = mouseY/(spacing+rectSize);

  if (redX>-1 && redX<selected.length && redY>-1 && redY<selected[redY].length)
  selected[redX][redY]=!selected[redX][redY];

  //buttons
  if(dist(mouseX,mouseY,310,2*height/3+40)<40) noLoop();         //pause
  else if(dist(mouseX,mouseY,415,2*height/3+40)<40) loop();      //start
  else if(dist(mouseX,mouseY,520,2*height/3+40)<40) t=0;         //restart: change the variables to the initial value
}
Tagged:

Answers

  • Hi,

    Nice sketch!

    As you said it is null because those values have not been instantiated before the double for loop. So, make sure you never call a sin[] element that is null. This is a simple way to achieve that:

    void draw()
    {
      if (frameCount%20==1)
      {
        t=(t+1)%16;
    
        for (int y=0; y < 5; y++)
        {
          if (selected[t][y])
          {
            sin[y] = new SinOsc(this);
            sin[y].freq(notes[y]);
            sin[y].play();
          }
          else if(sin[y]!= null) sin[y].stop();    //a fix
        }
      }
    // (...)
    
  • Thanks sooooo much! I've thought about the similar idea, but I may be insufficient to understand fully the "!"means, so I wrote

    else if( !selected[t][y] ) sin[y].stop();

    and it does not work either. But yours works perfectly. I think I know where the problem is, cos if selected[t][y] is null, !selected[t][y] is null as well, so it does not help me..

Sign In or Register to comment.