我正在尝试从以下API解析JSON:https: //opentdb.com/api.php?amount = 1
但是当我试图获取问题值时,我收到以下错误:
Exception in thread "main" java.lang.NullPointerException
Run Code Online (Sandbox Code Playgroud)
我用这个代码:
public static void main(String[] args) throws IOException {
String question;
String sURL = "https://opentdb.com/api.php?amount=1"; //just a string
// Connect to the URL using java's native library
URL url = new URL(sURL);
URLConnection request = url.openConnection();
request.connect();
// Convert to a JSON object to print data
JsonParser jp = new JsonParser(); //from gson
JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent())); //Convert the input stream to a json element
JsonObject …Run Code Online (Sandbox Code Playgroud)