We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, I'm trying to load data from the PokeAPI into an array so I can have an array containing each pokemon. Here is my code:
import http.requests.*;
class Pokemon {
int pid, pheight, pweight;
String pname, front;
}
Pokemon Stats[] = new Pokemon[151];
for (int i = 1; i <= 151 ; i++){
GetRequest get = new GetRequest("http://"+"pokeapi.co/api/v2/pokemon/" + i);
get.send();
JSONObject stat = parseJSONObject(get.getContent());
Stats[i].pname = stat.getString("name");
Stats[i].pid = stat.getInt("id");
Stats[i].pheight = stat.getInt("height");
Stats[i].pweight = stat.getInt("weight");
Stats[i].front = stat.getString("front_default");
delay(15000);
}
It throws up a nullpointerexception during the loop on the getString function, and I can't for the life of me figure out why. Any tips? Sorry if this is a stupid mistake I've never done anything like this before.
Answers
You should do this first:
println(get.getContent());
You will see that name is not available in the top level of the JSON object. You will need to request the right field in the returned data. You will need to revise the API from the pokemon site to make sure you are requesting the right information. This is what I am getting from the request:
You an see that there are different names in the response. You can also check previous posts: https://forum.processing.org/two/search?Search=pokemon
Kf
https://Processing.org/reference/JSONObject_isNull_.html