小编kri*_*fer的帖子

Java从URL NullPointerException解析JSON

我正在尝试从以下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)

java parsing json nullpointerexception

2
推荐指数
1
解决办法
217
查看次数

标签 统计

java ×1

json ×1

nullpointerexception ×1

parsing ×1