# Variable and Function Questions

**URL:** https://discourse.processing.org/t/variable-and-function-questions/29636
**Category:** Coding Questions
**Tags:** homework
**Created:** [April 25, 2021, 5:31pm UTC](https://discourse.processing.org/t/variable-and-function-questions/29636 "2021-04-25T17:31:59Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![hklotz13](https://avatars.discourse-cdn.com/v4/letter/h/c77e96/32.png) [@hklotz13](https://discourse.processing.org/u/hklotz13)
#### Post date: [April 25, 2021, 5:31pm UTC](https://discourse.processing.org/t/variable-and-function-questions/29636/1 "2021-04-25T17:31:59Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

Hi there folks. I’m very much a beginner, and I’m currently very lost trying to troubleshoot a project I’m working on for a class. Essentially I’m just trying to make a bar graph with JSON from my Spotify account. I keep getting two errors: “the variable ‘i’ does not exist” and “the method getJSONObject(String) in the type JSONObject is not applicable for the arguments (int)”. I’ve been following a tutorial from my professor in which writes similar code, but for some reason it isn’t working for me. Here’s my code:  
\</\>import processing.data.JSONObject;  
void setup(){  
size(800,800);  
}  
void draw() {  
JSONObject spotify = loadJSONObject(“StreamingHistory0.json”);  
JSONArray tracks = spotify.getJSONArray(“msPlayed”);  
for(int i = 0; i \< spotify.size(); i++){  
JSONObject thisTrack = spotify.getJSONObject(i);  
println(thisTrack.getFloat(“msPlayed”));  
}  
rect(50\*i,height,-40,-100);  
}\</\>

Can anyone help me figure out where I went wrong? Thank you!

---

<div class="post-metadata">

### Author: ![th75](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@th75](https://discourse.processing.org/u/th75)
#### Post date: [April 25, 2021, 6:03pm UTC](https://discourse.processing.org/t/variable-and-function-questions/29636/2 "2021-04-25T18:03:06Z")

</div>

i will not give answers here,  
but i m sure you can find out the problem with those questions:  
you want do draw bar graph, usually few rectangles, how many times do you call rect() in your code?  
i guess rect() need to be different size, what is the variable changing rect size, where this variable come from?  
when you do println(), do you get the values expected?  
and last what is the purpose of tracks? where do you use it?

let s start with that,

---

<div class="post-metadata">

### Author: ![hklotz13](https://avatars.discourse-cdn.com/v4/letter/h/c77e96/32.png) [@hklotz13](https://discourse.processing.org/u/hklotz13)
#### Post date: [April 25, 2021, 6:19pm UTC](https://discourse.processing.org/t/variable-and-function-questions/29636/3 "2021-04-25T18:19:46Z")

</div>

In the JSON file “StreamingHistory0” it lists “endTime”, “artistName”, “trackName”, and “msPlayed”. I’m pretty confused because I thought I should make “i” represent “msPlayed”, but should it instead represent the number of each track in the array? I want the height of each bar to be changed by “msPlayed”, but I’m not sure what the x-axis should be. “tracks” was just a name for the variable representing the number of a track in the array.  
As far as println() it worked when I was using “trackName” and “artistName”, but when I changed my code to what it currently is it no longer displays anything but errors. I wanted it to display “msPlayed”.  
I’m not sure if I’m making sense, so apologies if I’m losing you. I have no coding experience and I am DEEPLY confused.

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [April 25, 2021, 7:45pm UTC](https://discourse.processing.org/t/variable-and-function-questions/29636/4 "2021-04-25T19:45:22Z")

</div>

Hello hklotz13,

Could you please edit your post and format your code by clicking this button: `</>` Thank you.

When you do this:

```auto
for(int i = 0; i < spotify.size(); i++){
  ...
}

```

You are declaring the variable `i` as an `int` in the scope of your `for loop`.  
As soon as you get out of the for loop, that variable i does not exist anymore.

So when your do `rect(50*i,height,-40,-100);` after you `for loop` the program doesn’t know what value i should be. And neither do you actually =)

For the second error, it happens on this line:

```auto
JSONObject thisTrack = spotify.getJSONObject(i);

```

As declared in your for loop, `i` is an integer. So you are calling the function `getJSONObject` and giving it an `integer`.  
The error you have is simply telling you that you can’t do that because this function was never defined. The only one that was defined was the one where you would give a `String` instead of an `integer`

---

<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: [April 25, 2021, 9:59pm UTC](https://discourse.processing.org/t/variable-and-function-questions/29636/5 "2021-04-25T21:59:53Z")

</div>

Hello,

It will be up to 30 days to get my data!

I found some sample data and worked with that.

I then used the example here:  
_[loadJSONArray() \ Language (API) \ Processing 3+](https://processing.org/reference/loadJSONArray_.html)_

I did modify the code for the rectangle.

And voila!

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/34dbcca76e99db83b27d864bd43d258eaf896748.png)

Keep trying!

`:)`

References:  
_[JSONObject \ Language (API) \ Processing 3+](https://processing.org/reference/JSONObject.html)_  
_[Data \ Processing.org](https://processing.org/tutorials/data/)_

---

<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: [September 22, 2025, 12:49pm UTC](https://discourse.processing.org/t/variable-and-function-questions/29636/7 "2025-09-22T12:49:57Z")

</div>

Hello @Adamcox,

> [@Adamcox](#):
>
> I am also trying to create a bar graph using JSON from my Spotify account

Cool beans!

This is an old topic.  
Please open a separate topic.

You can reference this one:

> [@Variable and Function Questions](https://discourse.processing.org/t/variable-and-function-questions/29636/5):
>
> Hello, It will be up to 30 days to get my data! I found some sample data and worked with that. I then used the example here: [loadJSONArray() \ Language (API) \ Processing 3+](https://processing.org/reference/loadJSONArray_.html) I did modify the code for the rectangle. And voila! Keep trying! slight_smile References: [JSONObject \ Language (API) \ Processing 3+](https://processing.org/reference/JSONObject.html)[Data \ Processing.org](https://processing.org/tutorials/data/)

`:)`

---

<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: [September 22, 2025, 12:54pm UTC](https://discourse.processing.org/t/variable-and-function-questions/29636/8 "2025-09-22T12:54:39Z")

</div>

> [@glv](#):
>
> References:  
> _[JSONObject \ Language (API) \ Processing 3+](https://processing.org/reference/JSONObject.html)_  
> _[Data \ Processing.org](https://processing.org/tutorials/data/)_

Update:

> **[Data \\ Processing.org](https://web.archive.org/web/20210414034754/https://processing.org/tutorials/data/)**
>
> Processing is a flexible software sketchbook and a language for learning how to code within the context of the visual arts. Since 2001, Processing has promoted software literacy within the visual arts and visual literacy within technology.

This is an old topic and ONLY updating the resource link.

Please start a new topic. I won’t be responding to this one.

`:)`
