在Servlet中出现空指针如何处理?

发布于 2022-09-06 13:22:03 字数 5510 浏览 12 评论 0

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
    ]
  }
}

图片描述

图片描述

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你列表最软的妹 2022-09-13 13:22:03

检查一下路径
String fileName01="src/main/java/com/mvc/telecomData/dataTime01.json";

冰雪梦之恋 2022-09-13 13:22:03

把路径改为
String fileName01="../graduation03/src/main/java/com/mvc/telecomData/dataTime01.json";

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文