线程“main”中的异常java.lang.NullPointerException at kkkkkkkkkkk.main.main(main.java:34)
private static final String filePath = "http://ksupulse.tk/get_all.php";
public static void main(String[] args) throws org.json.simple.parser.ParseException {
try {
URL serverAddress = new URL(filePath);
HttpURLConnection connection = (HttpURLConnection) serverAddress.openConnection();
connection.connect();
int rc = connection.getResponseCode();
if(rc == 200) {
String line = null;
BufferedReader reader = new BufferedReader(new java.io.InputStreamReader(connection.getInputStream()));
StringBuilder sb = new StringBuilder();
while((line = reader.readLine()) != null)
sb.append(line + '\n');
JSONObject obj = (JSONObject) JSONValue.parse(sb.toString());
//error
JSONArray array = (JSONArray) obj.get("response"); //<-------err this line
for(int i = 0; i < array.size(); i++) {
JSONObject list = (JSONObject) ((JSONObject)array.get(i)).get("list");
System.out.println(list.get("id")+": "+list.get("name"));
}
} else {
System.out.println("Connect error");
}
connection.disconnect();
} catch (java.net.MalformedURLException e) {
e.printStackTrace();
} catch (java.net.ProtocolException e) {
e.printStackTrace();
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
答案 0 :(得分:1)
你可以提供json文本吗?这是由于json文本解析不当造成的。错误是&#34;空指针异常&#34; 。当您尝试访问某些未定义的资源时会发生这些类型的错误。调试这个的一个好方法是使用一般异常并尝试找到确切的错误。您的代码只处理3个错误。您还需要处理其他错误。试试这个。
try{
//Some code
}(Exception e){
System.out.println("Error:"+e.toString());
}