My problem:
I need to read a JSON that is in a certain URL. I tried the following code, but it does not work:
JSONObject jsonObjeto;
JSONParser parser = new JSONParser();
URL url = new URL("http://www.exemplo.br/teste.json");
String x = url.openStream().toString();
Reader reader = new InputStreamReader(getClass().getResourceAsStream(x));
jsonObjeto = (JSONObject) parser.parse(reader);
I get the following error:
Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
Notes:
- This code is an example and does not include
try{}catch(...){}
in the example, does anyone have any idea how to solve this problem or why it is occurring? - I tried to use
String x = url.toString()
but also got the same error.