Exception in thread "main" java.lang.NullPointerException報(bào)錯(cuò)怎么解決
package bean;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.json.JSONException;
import org.json.JSONObject;
public class ReadJson {
?? ?public static void main(String[] args) throws IOException, JSONException{
?? ??? ?
?? ??? ?File fi = new File(ReadJson.class.getResource("/wangxiaoer.json").getFile());
?? ?
?? ??? ?String content = FileUtils.readFileToString(fi);
?? ??? ?JSONObject JO= new JSONObject(content);
?? ???? System.out.println("姓名是:"+JO.getString("name"));
?? ?}
}
2021-02-18
????? ? //ReadJSONSample.class.getResource 為項(xiàng)目的資源目錄路徑,請(qǐng)保證文件存在
????????File file=new File(ReadJSONSample.class.getResource("/wangxiaoer.json").getFile());?
????????
????????// 或者指定文件目錄,請(qǐng)保證文件存存
? ? ? ? //File file = new File("/tmp/wangxiaoer.json");
????????//
? ? ? ?String content = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
? ? ? ?JSONObject jsonObject = new JSONObject(content);
? ? ? ? System.out.println("name is " + jsonObject.getString("name"));
? ? ? ? System.out.println("age is " + jsonObject.getDouble("age"));
2019-02-17
把