#vulnerable code 
                                public SourceCodeDTO saveCode(CodeIndexDocument codeIndexDocument) {

        Optional<SourceCodeDTO> existing = this.getByCodeIndexDocument(codeIndexDocument);

        Connection conn = null;
        PreparedStatement stmt = null;

        try {
            conn = this.dbConfig.getConnection();
            String query = "INSERT INTO `sourcecode` (`id`, `repoid`, `languageid`, `sourceid`, `ownerid`, `licenseid`, `location`, `filename`, `content`, `hash`, `simhash`, `linescount`, `data`) VALUES " +
                    "(NULL, ?, (SELECT id FROM languagetype WHERE type = ?), ?, ?, ?, ?, ?, COMPRESS(?), ?, ?, ?, ?)";

            if (existing.isPresent()) {
                return existing.get();
            }

            stmt = conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
            stmt.setInt(1, 31337);
            stmt.setString(2, codeIndexDocument.getLanguageName());
            stmt.setInt(3, 31337);
            stmt.setInt(4, 31337);
            stmt.setInt(5, 31337);
            stmt.setString(6, this.getLocation(codeIndexDocument));
            stmt.setString(7, codeIndexDocument.getFileName());
            stmt.setString(8, codeIndexDocument.getContents());
            stmt.setString(9, codeIndexDocument.getHash());
            stmt.setString(10, "simhash");
            stmt.setInt(11, codeIndexDocument.getCodeLines());
            stmt.setString(12, "{}");

            stmt.execute();
            ResultSet tableKeys = stmt.getGeneratedKeys();
            tableKeys.next();
            int autoGeneratedID = tableKeys.getInt(1);
            return this.getById(autoGeneratedID).get();
        }
        catch (SQLException ex) {
            Singleton.getLogger().severe(" caught a " + ex.getClass() + "\n with message: " + ex.getMessage());
        }
        finally {
            this.helpers.closeQuietly(stmt);
//            this.helpers.closeQuietly(conn);
        }

        return null;
    } 
                            #location 41 
                            #vulnerability type NULL_DEREFERENCE
#fixed code
                                public SourceCodeDTO saveCode(CodeIndexDocument codeIndexDocument) {

        Optional<SourceCodeDTO> existing = this.getByCodeIndexDocument(codeIndexDocument);

        Connection conn = null;
        PreparedStatement stmt = null;

        try {
            conn = this.dbConfig.getConnection();

            // If the language does not exist then create it
            Optional<LanguageTypeDTO> languageType = this.languageType.createLanguageType(codeIndexDocument.getLanguageName());

            String query = "INSERT INTO `sourcecode` (`id`, `repoid`, `languageid`, `sourceid`, `ownerid`, `licenseid`, `location`, `filename`, `content`, `hash`, `simhash`, `linescount`, `data`) VALUES " +
                    "(NULL, ?, ?, ?, ?, ?, ?, ?, COMPRESS(?), ?, ?, ?, ?)";

            // Why is this here and not above??
            if (existing.isPresent()) {
                return existing.get();
            }

            stmt = conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
            stmt.setInt(1, 31337);
            stmt.setInt(2, languageType.get().getId());
            stmt.setInt(3, 31337);
            stmt.setInt(4, 31337);
            stmt.setInt(5, 31337);
            stmt.setString(6, this.getLocation(codeIndexDocument));
            stmt.setString(7, codeIndexDocument.getFileName());
            stmt.setString(8, codeIndexDocument.getContents());
            stmt.setString(9, codeIndexDocument.getHash());
            stmt.setString(10, "simhash");
            stmt.setInt(11, codeIndexDocument.getCodeLines());
            stmt.setString(12, "{}");

            stmt.execute();
            ResultSet tableKeys = stmt.getGeneratedKeys();
            tableKeys.next();
            int autoGeneratedID = tableKeys.getInt(1);
            return this.getById(autoGeneratedID).get();
        }
        catch (SQLException ex) {
            Singleton.getLogger().severe(" caught a " + ex.getClass() + "\n with message: " + ex.getMessage());
        }
        finally {
            this.helpers.closeQuietly(stmt);
//            this.helpers.closeQuietly(conn);
        }

        return null;
    }