Variable and Function Questions

please format code with </> button * homework policy * asking questions

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!

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,

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.

Hello hklotz13,

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

When you do this:

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:

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

1 Like

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+

I did modify the code for the rectangle.

And voila!

Keep trying!

:)

References:
JSONObject \ Language (API) \ Processing 3+
Data \ Processing.org