#vulnerable code 
                                private void handleBodyFetch(MimeMessage mimeMessage,
                                 String sectionSpecifier,
                                 String partial,
                                 StringBuilder response) throws IOException, MessagingException {
        if (sectionSpecifier.length() == 0) {
            // TODO - need to use an InputStream from the response here.
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            mimeMessage.writeTo(bout);
            byte[] bytes = bout.toByteArray();
            bytes = doPartial(partial, bytes, response);
            addLiteral(bytes, response);
        } else if ("HEADER".equalsIgnoreCase(sectionSpecifier)) {
            Enumeration<?> inum = mimeMessage.getAllHeaderLines();
            addHeaders(inum, response);
        } else if (sectionSpecifier.startsWith("HEADER.FIELDS.NOT")) {
            String[] excludeNames = extractHeaderList(sectionSpecifier, "HEADER.FIELDS.NOT".length());
            Enumeration<?> inum = mimeMessage.getNonMatchingHeaderLines(excludeNames);
            addHeaders(inum, response);
        } else if (sectionSpecifier.startsWith("HEADER.FIELDS ")) {
            String[] includeNames = extractHeaderList(sectionSpecifier, "HEADER.FIELDS ".length());
            Enumeration<?> inum = mimeMessage.getMatchingHeaderLines(includeNames);
            addHeaders(inum, response);
        } else if (sectionSpecifier.endsWith("MIME")) {
            String[] strs = sectionSpecifier.trim().split("\\.");
            int partNumber = Integer.parseInt(strs[0]) - 1;
            MimeMultipart mp = (MimeMultipart) mimeMessage.getContent();
            byte[] bytes = GreenMailUtil.getHeaderAsBytes(mp.getBodyPart(partNumber));
            bytes = doPartial(partial, bytes, response);
            addLiteral(bytes, response);
        } else if ("TEXT".equalsIgnoreCase(sectionSpecifier)) {
            handleBodyFetchForText(mimeMessage, partial, response);
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Fetching body part for section specifier " + sectionSpecifier +
                        " and mime message (contentType=" + mimeMessage.getContentType());
            }
            String contentType = mimeMessage.getContentType();
            if (contentType.startsWith("text/plain") && "1".equals(sectionSpecifier)) {
                handleBodyFetchForText(mimeMessage, partial, response);
            } else {
                MimeMultipart mp = (MimeMultipart) mimeMessage.getContent();
                BodyPart part = null;
                String[] nestedIdx = sectionSpecifier.split("\\.");
                for (String idx : nestedIdx) {
                    int partNumber = Integer.parseInt(idx) - 1;
                    if (null == part) {
                        part = mp.getBodyPart(partNumber);
                    } else {
                        // Content must be multipart
                        part = ((Multipart) part.getContent()).getBodyPart(partNumber);
                    }
                }
                byte[] bytes = GreenMailUtil.getBodyAsBytes(part);
                bytes = doPartial(partial, bytes, response);
                addLiteral(bytes, response);
            }
        }
    } 
                            #location 53 
                            #vulnerability type NULL_DEREFERENCE
#fixed code
                                private void handleBodyFetch(MimeMessage mimeMessage,
                                 String sectionSpecifier,
                                 String partial,
                                 StringBuilder response) throws IOException, MessagingException {
        if (sectionSpecifier.length() == 0) {
            // TODO - need to use an InputStream from the response here.
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            mimeMessage.writeTo(bout);
            byte[] bytes = bout.toByteArray();
            bytes = doPartial(partial, bytes, response);
            addLiteral(bytes, response);
        } else if ("HEADER".equalsIgnoreCase(sectionSpecifier)) {
            Enumeration<?> inum = mimeMessage.getAllHeaderLines();
            addHeaders(inum, response);
        } else if (sectionSpecifier.startsWith("HEADER.FIELDS.NOT")) {
            String[] excludeNames = extractHeaderList(sectionSpecifier, "HEADER.FIELDS.NOT".length());
            Enumeration<?> inum = mimeMessage.getNonMatchingHeaderLines(excludeNames);
            addHeaders(inum, response);
        } else if (sectionSpecifier.startsWith("HEADER.FIELDS ")) {
            String[] includeNames = extractHeaderList(sectionSpecifier, "HEADER.FIELDS ".length());
            Enumeration<?> inum = mimeMessage.getMatchingHeaderLines(includeNames);
            addHeaders(inum, response);
        } else if (sectionSpecifier.endsWith("MIME")) {
            String[] strs = sectionSpecifier.trim().split("\\.");
            int partNumber = Integer.parseInt(strs[0]) - 1;
            MimeMultipart mp = (MimeMultipart) mimeMessage.getContent();
            byte[] bytes = GreenMailUtil.getHeaderAsBytes(mp.getBodyPart(partNumber));
            bytes = doPartial(partial, bytes, response);
            addLiteral(bytes, response);
        } else if ("TEXT".equalsIgnoreCase(sectionSpecifier)) {
            handleBodyFetchForText(mimeMessage, partial, response);
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Fetching body part for section specifier " + sectionSpecifier +
                        " and mime message (contentType=" + mimeMessage.getContentType());
            }
            String contentType = mimeMessage.getContentType();
            if (contentType.toLowerCase().startsWith("text/plain") && "1".equals(sectionSpecifier)) {
                handleBodyFetchForText(mimeMessage, partial, response);
            } else {
                MimeMultipart mp = (MimeMultipart) mimeMessage.getContent();
                BodyPart part = null;

                // Find part by number spec, eg "1" or "2.1" or "4.3.1" ...
                String spec = sectionSpecifier;

                int dotIdx = spec.indexOf('.');
                String pre = dotIdx < 0 ? spec : spec.substring(0, dotIdx);
                while (null != pre && NUMBER_MATCHER.matcher(pre).matches()) {
                    int partNumber = Integer.parseInt(pre) - 1;
                    if (null == part) {
                        part = mp.getBodyPart(partNumber);
                    } else {
                        // Content must be multipart
                        part = ((Multipart) part.getContent()).getBodyPart(partNumber);
                    }

                    dotIdx = spec.indexOf('.');
                    if (dotIdx > 0) { // Another sub part index?
                        spec = spec.substring(dotIdx + 1);
                        pre = spec.substring(0, dotIdx);
                    } else {
                        pre = null;
                    }
                }

                if (null == part) {
                    throw new IllegalStateException("Got null for " + sectionSpecifier);
                }

                // A bit optimistic to only cover theses cases ... TODO
                if ("message/rfc822".equalsIgnoreCase(part.getContentType())) {
                    handleBodyFetch((MimeMessage) part.getContent(), spec, partial, response);
                } else if ("TEXT".equalsIgnoreCase(spec)) {
                    handleBodyFetchForText(mimeMessage, partial, response);
                } else {
                    byte[] bytes = GreenMailUtil.getBodyAsBytes(part);
                    bytes = doPartial(partial, bytes, response);
                    addLiteral(bytes, response);
                }
            }
        }
    }