0

I want to get the currency's from Json using Gson but my method returns null. The currency value should be a BigDecimal object and I want to get the value using currency symbol as method argument.

I've created a method rateFromApi(String currency) shown below. I've read the API and from what I understood the line

rate = rootObj.get(currency).getAsBigDecimal();

should assign to variable rate the value to which currency name is mapped. I found similar problem already here But still I do not understand what am I doing wrong in my code.

//fields and method in class responsible for getting data from Json
private final String sURL = "http://data.fixer.io/api/latest?access_key...";
    private URL url = new URL(sURL);
    private URLConnection request = url.openConnection();
    private JsonParser jp = new JsonParser();

public BigDecimal rateFromAPI(String currency) throws IOException {
        request.connect();
        BigDecimal rate;
        JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent()));
        JsonObject rootObj = root.getAsJsonObject();
        return rate = rootObj.get(currency).getAsBigDecimal();
    }


//expression in method main
//indicate which exactly currency has to be taken from Json
Zloty pln1 = eur.convertToPln(ratesGetter.rateFromAPI("PLN"));

I thought the Json will be parsed and by using the line rootObj.get(currency).getAsBigDecimal(); the value which is mapped to the currency name will be assigned to variable rate as BigDecimal but the result I'm getting is NullPointerException in return line.

novice
  • 41
  • 4
  • 3
    You might want to redact the access key. – chessofnerd Jul 07 '19 at 20:14
  • and then include a dummy json that you would get returned if you actually accessed the API. – luk2302 Jul 07 '19 at 20:14
  • Open http://data.fixer.io/api/latest?access_key=&format=1. Look: it doesn't have any "PLN" property. It has a "rates" property, which itself has a "PLN" property. – JB Nizet Jul 07 '19 at 20:15
  • Are you sure it's `get(currency)`, and not `get("currency")`? If you've posted a snippet, please make sure it's a [mcve], by testing it to ensure it works and demonstrates the issue. Ideally, embed some sample JSON as a string literal, instead of depending on a network call. – Nic Jul 07 '19 at 22:10
  • @Nic Hartley I'm pretty sure it should be `get(currency)` instead of `get("currency")` because I'm passing String in this method somewhere else like here `eur.convertToPln(ratesGetter.rateFromAPI("PLN"));` – novice Jul 08 '19 at 14:47
  • So it's not an MCVE, then, because this won't compile as-is (there's no `currency` variable defined in the snippet). Please write a [mcve] and [edit] your question to have it, instead of a snippet. – Nic Jul 08 '19 at 19:38
  • Thank you all for help, I think I found the problem, as @JB Nizet suggested I had to get to "PLN" property instead "rates". One additional line of code in method `ratesFromAPI()` solved the issue.This line is `JsonObject ratesList = rootObj.getAsJsonObject("rates");` and then `return ratesList.get(currency).getAsBigDecimal();` – novice Jul 09 '19 at 00:56

0 Answers0