# Having trouble with a code and its structure

**URL:** <https://discourse.processing.org/t/having-trouble-with-a-code-and-its-structure/25533>\
**Category:** Libraries\
**Tags:** homework\
**Created:** [November 19, 2020, 3:30pm UTC](https://discourse.processing.org/t/having-trouble-with-a-code-and-its-structure/25533 "2020-11-19T15:30:17Z")\
**Posts on this page:** 14\
**Page:** 2

<div class="post-metadata">

**Author:** ![Spomch](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@Spomch](https://discourse.processing.org/u/Spomch)\
**Post date:** [November 22, 2020, 10:46pm UTC](https://discourse.processing.org/t/having-trouble-with-a-code-and-its-structure/25533/21 "2020-11-22T22:46:39Z")

</div>

Ok so this is the updated sketch :

```auto
import processing.sound.*;
SoundFile Son;
PImage portrait;
PImage telephone;
PImage mail;
PImage arme;
int rand;
int action;
int savedTimeSon;
int savedTimeImage;
int soundTime = 500;
int imageTime = 500;
int state = 0;

void setup() {
  size(640, 640);
  telephone = loadImage("Calls.jpg");
  mail = loadImage("Emails.jpg");
  arme = loadImage("Kills.jpg");
  rand = int(random(1, 155));
}

void draw() {
  
if (state == 0) {
  takerandomimage("portrait" + nf(rand, 3) + ".jpg");
  action = int (random (1, 4));
  if(action==1) {
    image (telephone, 0, 0);
    Son = new SoundFile(this, "Son_calls.mp3");
    Son.play();
  } else if(action==2) {
    image (mail, 0, 0);
    Son = new SoundFile(this, "Son_emails.mp3");
    Son.play();
  } else if(action==3) {
    image (arme, 0, 0);
    Son = new SoundFile(this, "Son_kills.mp3");
    Son.play();
  }
  state ++;
}

else if (state == 1) {
  if (!Son.isPlaying()){
    state = 2;
    }
  }
  
else if (state == 2) {  
  if (!Son.isPlaying()){
    state = 3;
    }
  }
  
else if (state == 3) {  
  if (!Son.isPlaying()){
    state = 4;
    }
  }
}

void takerandomimage(String fn) {
  portrait = loadImage(fn);
  image(portrait, 0, 0);
}

```

This time :

- Only one “action” image appears
- But the sound is synced and has no glitches, which has never happened before, so this is good !

But the portrait images never seem to show up, and the program only runs once.  
I guess that it has to do with this part of the sketch that I don’t fully understand :

```auto
 } else if (state == 1) {
    if (!Son.isPlaying()) {
      state = 2;
    }
  } else if (state == 2) {  
    if (!Son.isPlaying()) {
      state = 3;
    }
  } else if (state == 3) {  
    if (!Son.isPlaying()) {
      state = 4;
    }

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 22, 2020, 11:04pm UTC](https://discourse.processing.org/t/having-trouble-with-a-code-and-its-structure/25533/22 "2020-11-22T23:04:41Z")

</div>

> [@Spomch](#):
>
> ```auto
> else if (state == 2) {  
> if (!Son.isPlaying()){
> state = 3;
> }
> }
>   
> else if (state == 3) {  
> if (!Son.isPlaying()){
> state = 4;
> }
> }
> 
> ```

not necessary

instead

```auto
else if (state == 1) {
  if (!Son.isPlaying()){
    rand = int(random(1, 155)); // !!!!!!
    state = 0;
    }
  }

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 22, 2020, 11:05pm UTC](https://discourse.processing.org/t/having-trouble-with-a-code-and-its-structure/25533/23 "2020-11-22T23:05:08Z")

</div>

are you using background anywhere

better if you don’t

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 22, 2020, 11:07pm UTC](https://discourse.processing.org/t/having-trouble-with-a-code-and-its-structure/25533/24 "2020-11-22T23:07:29Z")

</div>

are you place the image for the action ABOVE the image for the portrait

both are at 0,0

move the image for the action to the right

```auto
image (telephone, 0, 0);

```

same for mail and telephone and so on

---

<div class="post-metadata">

**Author:** ![Spomch](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@Spomch](https://discourse.processing.org/u/Spomch)\
**Post date:** [November 22, 2020, 11:16pm UTC](https://discourse.processing.org/t/having-trouble-with-a-code-and-its-structure/25533/25 "2020-11-22T23:16:16Z")

</div>

I am not using background.  
When moving the images for the action, I can see that the image for the portrait is underneath the image for the action.  
So the issue is only about the length of display of the images.  
I am going to try to implement the timer function in the example that you have showed me before  
This one :

```auto
int state=0; 
int timer; 

void setup() {
  size(640, 640);

  timer = millis();
  background(0);
}

void draw() {
  background(0); 
  if (state == 0) {
    text("state is 0", 111, 111);
    if (millis()- timer > 3000) {
      state = 1;
    }
  } else if (state == 1) {
    text("state is 1", 111, 111);
  }
}

```

---

<div class="post-metadata">

**Author:** ![Spomch](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@Spomch](https://discourse.processing.org/u/Spomch)\
**Post date:** [November 23, 2020, 12:03am UTC](https://discourse.processing.org/t/having-trouble-with-a-code-and-its-structure/25533/26 "2020-11-23T00:03:10Z")

</div>

New version with an attempt to implement a timer :

```auto
import processing.sound.*;
SoundFile Son;
PImage portrait;
PImage telephone;
PImage mail;
PImage arme;
int rand;
int action;
int actionAllowed = 0;
int timer;
int state = 0;

void setup() {
  size(640, 640);
  telephone = loadImage("Calls.jpg");
  mail = loadImage("Emails.jpg");
  arme = loadImage("Kills.jpg");
  rand = int(random(1, 155));
  timer = millis();
}

void draw() {

  if (state == 0) {
    takerandomimage("portrait" + nf(rand, 3) + ".jpg");
    image (portrait, 0, 0);
    if (actionAllowed == 0) {
      if (millis()- timer > 5) {
        actionAllowed = 1;
      }
    } else if (actionAllowed == 1) {
      action = int (random (1, 4));
      if (action==1) {
        image (telephone, 0, 0);
        Son = new SoundFile(this, "Son_calls.mp3");
        Son.play();
      } else if (action==2) {
        image (mail, 0, 0);
        Son = new SoundFile(this, "Son_emails.mp3");
        Son.play();
      } else if (action==3) {
        image (arme, 0, 0);
        Son = new SoundFile(this, "Son_kills.mp3");
        Son.play();
      }
    }
    state ++;
  } else if (state == 1) {
    if (!Son.isPlaying()) {
      state = 2;
    }
  } else if (state == 2) {  
    if (!Son.isPlaying()) {
      state = 3;
    }
  } else if (state == 3) {  
    if (!Son.isPlaying()) {
      state = 4;
    }
  }
}

void takerandomimage(String fn) {
  portrait = loadImage(fn);
  image(portrait, 0, 0);
}

```

Here, the program only shows one portrait before indicating “NullPointerException”.  
I felt like my attempt made sense (logic wise), I’m kind of lost right now.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 23, 2020, 1:30am UTC](https://discourse.processing.org/t/having-trouble-with-a-code-and-its-structure/25533/27 "2020-11-23T01:30:59Z")

</div>

Where does the nullpointer occur?

---

<div class="post-metadata">

**Author:** ![Spomch](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@Spomch](https://discourse.processing.org/u/Spomch)\
**Post date:** [November 23, 2020, 9:52am UTC](https://discourse.processing.org/t/having-trouble-with-a-code-and-its-structure/25533/28 "2020-11-23T09:52:53Z")

</div>

It occurs at :

```auto
    if (!Son.isPlaying()) {

```

 ![Capture d’écran 2020-11-23 à 10.51.44](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/561ee27860f39f52c24bca6faf8c125376a5d5f2.png)

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 23, 2020, 10:48am UTC](https://discourse.processing.org/t/having-trouble-with-a-code-and-its-structure/25533/29 "2020-11-23T10:48:56Z")

</div>

When actionAllowed == 0 the code is executed but Son is not defined

You can say if(Son!=null) right before the if with the isPlaying…

The last case clauses are not needed as I wrote

---

<div class="post-metadata">

**Author:** ![Spomch](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@Spomch](https://discourse.processing.org/u/Spomch)\
**Post date:** [November 23, 2020, 11:47am UTC](https://discourse.processing.org/t/having-trouble-with-a-code-and-its-structure/25533/30 "2020-11-23T11:47:16Z")

</div>

> [@Chrisir](#):
>
> When actionAllowed == 0 the code is executed but Son is not defined

Yes but I cannot define Son this early in the program because the random choice has to be done before defining which SoundFile it is.

When you say :

> [@Chrisir](#):
>
> You can say if(Son!=null) right before the if with the isPlaying…

Do you mean like this ?

```auto
} else if (state == 1) {
    if (Son!=null) {
      if (!Son.isPlaying()) {
        state = 2;
      }
    }

```

And what do you mean by :

> [@Chrisir](#):
>
> The last case clauses are not needed as I wrote

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 23, 2020, 1:05pm UTC](https://discourse.processing.org/t/having-trouble-with-a-code-and-its-structure/25533/31 "2020-11-23T13:05:06Z")

</div>

> [@Spomch](#):
>
> Do you mean like this ?

Yes - that’s correct

> [@Spomch](#):
>
> And what do you mean by :

> [@Chrisir](#):
>
> > [@Spomch](#):
> >
> > ````auto
> > > else if (state == 2) {  
> > if (!Son.isPlaying()){
> > state = 3;
> > }
> > }
> >   
> > else if (state == 3) {  
> > if (!Son.isPlaying()){
> > state = 4;
> > }
> > }
> > > ```
> > 
> > ````
> 
> not necessary
> 
> instead
> 
> ```auto
> else if (state == 1) {
> if (!Son.isPlaying()){
> rand = int(random(1, 155)); // !!!!!!
> state = 0;
> }
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![Spomch](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@Spomch](https://discourse.processing.org/u/Spomch)\
**Post date:** [November 23, 2020, 1:33pm UTC](https://discourse.processing.org/t/having-trouble-with-a-code-and-its-structure/25533/32 "2020-11-23T13:33:34Z")

</div>

This is what I have :

```auto
import processing.sound.*;
SoundFile Son;
PImage portrait;
PImage telephone;
PImage mail;
PImage arme;
int rand;
int action;
int actionAllowed = 0;
int timer;
int state = 0;

void setup() {
  size(640, 640);
  telephone = loadImage("Calls.jpg");
  mail = loadImage("Emails.jpg");
  arme = loadImage("Kills.jpg");
  rand = int(random(1, 155));
  timer = millis();
}

void draw() {

  if (state == 0) {
    takerandomimage("portrait" + nf(rand, 3) + ".jpg");
    image (portrait, 0, 0);
    if (actionAllowed == 0) {
      if (millis()- timer > 500) {
        actionAllowed = 1;
      }
    } else if (actionAllowed == 1) {
      action = int (random (1, 4));
      if (action==1) {
        image (telephone, 0, 0);
        Son = new SoundFile(this, "Son_calls.mp3");
        Son.play();
      } else if (action==2) {
        image (mail, 0, 0);
        Son = new SoundFile(this, "Son_emails.mp3");
        Son.play();
      } else if (action==3) {
        image (arme, 0, 0);
        Son = new SoundFile(this, "Son_kills.mp3");
        Son.play();
      }
    }
    state ++;
  } else if (state == 1) {
    if (Son!=null) {
      if (!Son.isPlaying()) {
        rand = int(random(1, 155));
        state = 0;
      }
    }
  }
}

void takerandomimage(String fn) {
  portrait = loadImage(fn);
  image(portrait, 0, 0);
}

```

When I run the code, only one portrait appears

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [November 23, 2020, 2:27pm UTC](https://discourse.processing.org/t/having-trouble-with-a-code-and-its-structure/25533/33 "2020-11-23T14:27:45Z")

</div>

Hello,

You can use println() statements in your code to help debug; print a meaningful string or variable to help you know where you are in the code.

This is always _true_ 500ms after the start of the program:  
`millis()- timer > 500`

In this case 500ms after you initialized timer in setup():  
`timer = millis();`

Is this what you intended for the timer?

There are many examples of timers out there.

`:)`

---

<div class="post-metadata">

**Author:** ![Spomch](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@Spomch](https://discourse.processing.org/u/Spomch)\
**Post date:** [November 23, 2020, 3:49pm UTC](https://discourse.processing.org/t/having-trouble-with-a-code-and-its-structure/25533/34 "2020-11-23T15:49:19Z")

</div>

Thank you for your reply !  
I will try to use println() in the future.

> [@glv](#):
>
> There are many examples of timers out there.

Yes, I have read multiple threads and have seen multiple examples, but I feel like I am missing the logic of the timer.

> [@glv](#):
>
> Is this what you intended for the timer?

I think that I understand how my timer is currently understanding my orders. But I don’t think that it corresponds to what I intend for the timer.

What I would like to tell the program to do is :

- Display a random portrait
- Wait 0.5 seconds
- Display a random “action” image and play its sound
- Wait 0.5 seconds
- Repeat

But I don’t know how to change the timer so that it does what I want it to do.  
Thanks again !

[Previous page](https://discourse.processing.org/t/having-trouble-with-a-code-and-its-structure/25533.md?page=1)
