亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Exception in thread "main" java.lang.NullPointerException報錯怎么解決

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"));
?? ?}
}

正在回答

2 回答

????? ? //ReadJSONSample.class.getResource 為項目的資源目錄路徑,請保證文件存在

????????File file=new File(ReadJSONSample.class.getResource("/wangxiaoer.json").getFile());?

????????

????????// 或者指定文件目錄,請保證文件存存

? ? ? ? //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"));


0 回復 有任何疑惑可以回復我~

jsonContent.json
放到resouces?路徑下。在如下讀取
File?file?=?new?File(ReadJsonSample.class.getClassLoader().getResource("./jsonContent.json").getPath());


1 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

Exception in thread "main" java.lang.NullPointerException報錯怎么解決

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號