Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 阻止多线程TCP服务器NullPointerException调试_Java_Android_Debugging_Exception_Tcp - Fatal编程技术网

Java 阻止多线程TCP服务器NullPointerException调试

Java 阻止多线程TCP服务器NullPointerException调试,java,android,debugging,exception,tcp,Java,Android,Debugging,Exception,Tcp,我有一个TCP服务器,它接受数据并将其保存到文本文件中。然后,它使用该文本文件创建图像并将其发送回客户端。每隔几个小时,我就会得到一个NullPointerException,它会被抛出到此后连接的每个客户端。我不知道如何调试它,因为我无法自己复制它 有没有人有任何调试实践来帮助我弄清楚为什么这会成为一个问题 运行的服务器运行的是Ubuntu12.04 i386,内存为2 Gig。我最初的怀疑是,有些东西没有正确地关闭,并产生了问题,但就我所知,一切都应该关闭 ServerSocket

我有一个TCP服务器,它接受数据并将其保存到文本文件中。然后,它使用该文本文件创建图像并将其发送回客户端。每隔几个小时,我就会得到一个
NullPointerException
,它会被抛出到此后连接的每个客户端。我不知道如何调试它,因为我无法自己复制它

有没有人有任何调试实践来帮助我弄清楚为什么这会成为一个问题

运行的服务器运行的是Ubuntu12.04 i386,内存为2 Gig。我最初的怀疑是,有些东西没有正确地关闭,并产生了问题,但就我所知,一切都应该关闭

    ServerSocket echoServer = null;
    Socket clientSocket = null;
    try {
        echoServer = new ServerSocket(xxx);
    } catch (IOException e) {
        System.out.println(e);
    }

    while(true)
    {
            InputStream is = null;
            FileOutputStream fos = null;
            BufferedOutputStream bos = null;
            int bufferSize = 0;
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            BufferedOutputStream out = null;
        try {

            //Receieve text file
            is = null;
            fos = null;
            bos = null;
            bufferSize = 0;
            String uid = createUid();

            try {
                clientSocket = echoServer.accept();
                clientSocket.setKeepAlive(true);
                clientSocket.setSoTimeout(10000);
                System.out.println("Client accepted from: " + clientSocket.getInetAddress());
            } catch (IOException ex) {
                System.out.println("Can't accept client connection. ");
            }

            try {
                is = clientSocket.getInputStream();
                bufferSize = clientSocket.getReceiveBufferSize();
                System.out.println("Buffer size: " + bufferSize);
            } catch (IOException ex) {
                System.out.println("Can't get socket input stream. ");
            }

            try {
                fos = new FileOutputStream("/my/diretory/" + uid + ".txt");
                bos = new BufferedOutputStream(fos);

            } catch (FileNotFoundException ex) {
                System.out.println("File not found. ");
            }

            byte[] bytes = new byte[bufferSize];

            int count;

            while ((count = is.read(bytes)) > 0) {
                bos.write(bytes, 0, count);
                System.out.println("Receiving... " + count);
            }

            System.out.println("Done receiving text file");
            bos.flush();
            bos.close();
            fos.close();

            //image
            String[] command = new String[3];
            command[0] = "python";
            command[1] = "imagecreationfile.py";
            command[2] = uid;
            System.out.println("Starting python script");   
            Boolean success = startScript(command);
            if(success)
            {
                System.out.println("Script completed successfully");
                //Send image here
                String image = "/my/directory/" + uid + ".png";
                File imageFile = new File(image);
                long length = imageFile.length();
                if (length > Integer.MAX_VALUE) {
                    System.out.println("File is too large.");
                }

                bytes = new byte[(int) length];
                fis = new FileInputStream(imageFile);
                bis = new BufferedInputStream(fis);
                out = new BufferedOutputStream(clientSocket.getOutputStream());

                count = 0;
                while ((count = bis.read(bytes)) > 0) {
                    out.write(bytes, 0, count);
                    System.out.println("Writing... " + count);
                }

                out.flush();
                out.close();
                fis.close();
                bis.close();
            }
            else
            {
                System.out.println("Script failed");
            }


            System.out.println("Closing connection");
            is.close();
            clientSocket.close();
        } catch (Exception e) {
            System.out.println(e); //This is where the exception is being caught

        }

        if(!clientSocket.isClosed())
        {
            try {
                clientSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try {
        if(is != null)
            is.close();

        if(fos != null)
            fos.close();

        if(bos != null)
            bos.close();

        if(fis != null)
            fis.close();

        if(bis != null)
            bis.close();

        if(out != null)
            out.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

可能在您的一个try-catch作用域中引发了异常。 下一个try-catch作用域发现了空变量

比如说

//Receieve text file
        is = null;
        fos = null;
        bos = null;
        bufferSize = 0;
        String uid = createUid();

        try {
            clientSocket = echoServer.accept();
            clientSocket.setKeepAlive(true);
            clientSocket.setSoTimeout(10000);
            System.out.println("Client accepted from: " + clientSocket.getInetAddress());
        } catch (IOException ex) {
            System.out.println("Can't accept client connection. ");
        }

        try {
            is = clientSocket.getInputStream();
            bufferSize = clientSocket.getReceiveBufferSize();
            System.out.println("Buffer size: " + bufferSize);
        } catch (IOException ex) {
            System.out.println("Can't get socket input stream. ");
        }
如果在“clientSocket=echoServer.accept();”中抛出IOException,它将打印“无法接受客户端连接”。 当执行“is=clientSocket.getInputStream();”时,它将抛出NullPointer,因为“clientSocket”未正确初始化。
我的建议是,除非必要,否则不要在不同的try-catch作用域中打断已排序的语句。

发布堆栈跟踪,并确定哪个源行引发异常。没有这些,这只是一个猜测游戏。两种情况下的答案都是一样的。获取堆栈跟踪。没有它你什么都做不了,一旦你有了它,解决办法就会显而易见。