RyPope
RyPope

Reputation: 2725

Blocking multithreaded TCP Server NullPointerException debugging

I have a TCP server that accepts data and saves it to a text file. It then uses that text file to create an image and sends it back to the client. Every couple of hours I will get a NullPointerException that gets thrown to every client that connects after that. I am not sure how to go about debugging this as I cannot replicate it on my own.

Does anyone have any debugging practices to help me figure out why this is becoming a problem?

The server running is running Ubuntu 12.04 i386 with 2 gigs of RAM. My initial suspicion is that something is not getting closed properly and creating issues but everything should be getting closed as far as I can tell.

    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();
        }

    }

}

Upvotes: 0

Views: 130

Answers (1)

adt14
adt14

Reputation: 76

Maybe exception was thrown in one of your try-catch scope. And the next try-catch scope found null variables.

for example

//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. ");
        }

if IOException was thrown in "clientSocket = echoServer.accept();" , it will print "Can't accept client connection. ". When, "is = clientSocket.getInputStream();" executed, it will throw NullPointer because "clientSocket" was not initialized properly. My suggestion, dont break a sequenced statement in different try-catch scope until it necessary.

Upvotes: 1

Related Questions