抛出InputStream.read()方法的java.lang.NullPointer

时间:2016-11-06 09:34:06

标签: java http-proxy

我目前正在编写一个简单的代理服务器,它接受来自我的Web浏览器的连接。问题是我无法访问ice.xjtlu.edu.cn的学校网站,而我的朋友访问它时没有问题。它会在NullPointerException被调用的行处抛出read()

private static final int BUFFER_SIZE = 32768;
public void run() {

    try {
        DataOutputStream out = new DataOutputStream(socket.getOutputStream());
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); //convert input to string

        String temp = "";
        temp = in.readLine();
        String[] tempArr = temp.split(" ");

        System.out.println("Type of connection: " + tempArr[0]);
        System.out.println("URL of website: " + tempArr[1]);
        System.out.println(tempArr[2]);

        BufferedReader rd = null;
        try {
            System.out.println("Sending request to: " + tempArr[1]);

            URL url = new URL(tempArr[1]);
            URLConnection conn = url.openConnection();
            conn.setDoInput(true);
            // not doing HTTP posts
            conn.setDoOutput(false);

            InputStream is = null;
            HttpURLConnection huc = (HttpURLConnection) conn;
            if (huc.getContentLength() > 0) {
                try {
                    is = huc.getInputStream();
                    rd = new BufferedReader(new InputStreamReader(is));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            byte[] by = new byte[BUFFER_SIZE];

            int index = is.read(by, 0, BUFFER_SIZE);

            while (index != -1) {
                out.write(by, 0, index);
                index = is.read(by, 0, BUFFER_SIZE);
            }
            out.flush();
        } catch (IOException e) {
            System.out.println(e);
            System.out.println("is it this one");
            out.writeBytes("");
        }
      //close sockets

我已阅读 documentation 了解read()方法,NullPointerException仅在byte[]NULL时才会被抛出。
但是,如果byte[]NULL之前始终初始化为32768,那么read()怎么可能是ammendedInitialArray.remove(at: stepmax1) var max2 = Float(-1.0) var stepmax2 = Int(-1) for (index, value) in ammendedInitialArray.enumerated() { if value > max { max2 = value stepmax2 = index }

0 个答案:

没有答案