在Servlet中出现空指针如何处理?
package com.mvc.utils;import java.io.*;
public class JsonFileToStringUtil {
public static String fileToString() {
String fileName01="src/main/java/com/mvc/telecomData/dataTime01.json";
// 数据 - 1
File file = new File(fileName01);
StringBuffer sb = new StringBuffer();
String line;
BufferedReader br=null;
try {
br=new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
while((line=br.readLine())!=null){
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
String telecomDataJson01 = sb.toString();
return telecomDataJson01;
}
}
package com.mvc.servlet;import com.alibaba.fastjson.JSON;
import com.mvc.bean.Student;
import com.mvc.utils.JsonFileToStringUtil;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
public class ApiTest extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// response 反应; 回答
try {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
response.setContentType("text/html;charset=utf-8");
//response.setContentType("application/json;charset=utf-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("pragma", "no-cache");
response.setHeader("cache-control", "no-cache");
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Headers", "Authentication");
//传输JSON
// 文件解析成字符串
String jf = JsonFileToStringUtil.fileToString();
System.out.println(jf);
}catch (Exception e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
{ "code": 200,
"msg": "success",
"dataChinaTelecom":
{
"regionTime": [
"01-12 2:00", "01-12 3:00", "01-12 4:00", "01-12 5:00", "01-12 6:00",
"01-12 7:00", "01-12 8:00", "01-12 9:00", "01-12 10:00", "01-12 11:00",
"01-12 12:00", "01-12 13:00", "01-12 14:00", "01-12 15:00", "01-12 16:00",
"01-12 17:00", "01-12 18:00", "01-12 19:00", "01-12 20:00", "01-12 21:00",
"01-12 22:00", "01-12 23:00", "01-13 0:00", "01-13 1:00", "01-13 2:00",
"01-13 3:00"
],
"dataHttpHistory": [
55,
88,
95,
10,
50,
11,
5,
55,
35,
52,
67,
10,
5,
35,
55,
65,
71,
15,
51,
25,
66,
77,
88,
15,
65,
36
],
"dataPingDelayTime": [
55,
88,
95,
10,
50,
11,
5,
55,
35,
52,
67,
10,
5,
35,
55,
65,
71,
15,
51,
25,
66,
77,
88,
15,
65,
36
],
"dataPingLostPackets":[
55,
88,
95,
10,
50,
11,
5,
55,
35,
52,
67,
10,
5,
35,
55,
65,
71,
15,
51,
25,
66,
77,
88,
15,
65,
36
]
},
"dataChinaTelecom":
{
"regionTime":
[
"01-12 2:00",
"01-12 3:00",
"01-12 4:00",
"01-12 5:00",
"01-12 6:00",
"01-12 7:00",
"01-12 8:00",
"01-12 9:00",
"01-12 10:00",
"01-12 11:00",
"01-12 12:00",
"01-12 13:00",
"01-12 14:00",
"01-12 15:00",
"01-12 16:00",
"01-12 17:00",
"01-12 18:00",
"01-12 19:00",
"01-12 20:00",
"01-12 21:00",
"01-12 22:00",
"01-12 23:00",
"01-13 0:00",
"01-13 1:00",
"01-13 2:00",
"01-13 3:00"
],
"dataHttpHistory": [
55,
88,
95,
10,
50,
11,
5,
55,
35,
52,
67,
10,
5,
35,
55,
65,
71,
15,
51,
25,
66,
77,
88,
15,
65,
36
],
"dataPingDelayTime": [
55,
88,
95,
10,
50,
11,
5,
55,
35,
52,
67,
10,
5,
35,
55,
65,
71,
15,
51,
25,
66,
77,
88,
15,
65,
36
],
"dataPingLostPackets":[
55,
88,
95,
10,
50,
11,
5,
55,
35,
52,
67,
10,
5,
35,
55,
65,
71,
15,
51,
25,
66,
77,
88,
15,
65,
36
]
}
}
回答:
检查一下路径
String fileName01="src/main/java/com/mvc/telecomData/dataTime01.json";
回答:
把路径改为
String fileName01="../graduation03/src/main/java/com/mvc/telecomData/dataTime01.json";
以上是 在Servlet中出现空指针如何处理? 的全部内容, 来源链接: utcz.com/p/169236.html