#vulnerable code 
                                @Override
    public ByteBuf encodeResponse(Request request, Response response) {
        FullHttpRequest httpRequest = (FullHttpRequest) request.getMsg();
        FullHttpResponse httpResponse = null;

        try {
            if (response.getException() != null) {
                httpResponse =
                        new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
            } else {
                httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
                addHttpResponseHeaders(httpResponse, response, httpRequest);
                int protocolType = Integer.parseInt(httpRequest.headers().get(PROTOCOL_TYPE));
                Object body = makeResponse(protocolType, response);
                // encode body
                try {
                    byte[] responseBytes = encodeBody(protocolType,
                            httpRequest.headers().get(HttpHeaderNames.CONTENT_ENCODING),
                            body, response.getRpcMethodInfo());
                    httpResponse.content().writeBytes(responseBytes);
                    httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, responseBytes.length);
                } catch (Exception e) {
                    LOG.warn("encode response failed", e);
                    response.setException(e);
                    httpResponse =
                            new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
                }
            }
            // encode full http response
            BrpcHttpResponseEncoder encoder = new BrpcHttpResponseEncoder();
            return encoder.encode(httpResponse);
        } catch (Exception e) {
            LOG.warn("encode response failed", e);
            response.setException(e);
            return null;
        } finally {
            if (httpResponse != null) {
                httpResponse.release();
            }
        }
    } 
                            #location 21 
                            #vulnerability type NULL_DEREFERENCE
#fixed code
                                @Override
    public ByteBuf encodeResponse(Request request, Response response) {
        FullHttpRequest httpRequest = (FullHttpRequest) request.getMsg();
        FullHttpResponse httpResponse = null;

        try {
            byte[] responseBytes;
            if (response.getException() != null) {
                httpResponse = new DefaultFullHttpResponse(
                        HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
                responseBytes = response.getException().toString().getBytes();
            } else {
                httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
                int protocolType = Integer.parseInt(httpRequest.headers().get(PROTOCOL_TYPE));
                Object body = makeResponse(protocolType, response);
                // encode body
                try {
                    responseBytes = encodeBody(protocolType,
                            httpRequest.headers().get(HttpHeaderNames.CONTENT_ENCODING),
                            body, response.getRpcMethodInfo());
                } catch (Exception e) {
                    LOG.warn("encode response failed", e);
                    response.setException(e);
                    httpResponse = new DefaultFullHttpResponse(
                            HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
                    responseBytes = response.getException().toString().getBytes();
                }
            }
            httpResponse.content().writeBytes(responseBytes);
            addHttpResponseHeaders(httpResponse, response, httpRequest);
            // encode full http response
            BrpcHttpResponseEncoder encoder = new BrpcHttpResponseEncoder();
            return encoder.encode(httpResponse);
        } catch (Exception e) {
            LOG.warn("encode response failed", e);
            response.setException(e);
            return null;
        } finally {
            if (httpResponse != null) {
                httpResponse.release();
            }
        }
    }