使用我的资源文件夹中的JSON文件创建文件

时间:2018-03-04 16:37:14

标签: java json file nullpointerexception

Picture

^我的源文件夹的图片。

ObjectMapper jsonMapper = new ObjectMapper();
ClassLoader classLoader = getClass().getClassLoader();
File questionsFile = new 
File(classLoader.getResource("/Questions.json").getFile());

第4行抛出一个空指针异常,我不确定我做错了什么。

1 个答案:

答案 0 :(得分:0)

/Questions.json中的

/表示树根的根。你想要一个相对路径,试试

File questionsFile = new File(classLoader.getResource("Questions.json").getFile());