Band Select from Java/Python API

Hi, I’m trying to do the equivalent of Raster->Data Conversion->Band Select from a Python script using jpy to bridge the Java Api with Python.

The closest that I got was to use one of the snappy examples to create a rgb_image. The thing is that I need to create a new product with 4 bands and the method ProductUtils.createImageInfo only accepts 3 bands (the RGB). Is there another method to do almost the same but with more bands?

import os
import sys
# Imports Snap's module

import snappy
from snappy import jpy, Band, ProductIO, ProductUtils, ProgressMonitor
jpy = snappy.jpy

# More Java type definitions required for image generation
Color = jpy.get_type('java.awt.Color')
ColorPoint = jpy.get_type('org.esa.snap.core.datamodel.ColorPaletteDef$Point')
ColorPaletteDef = jpy.get_type('org.esa.snap.core.datamodel.ColorPaletteDef')
ImageInfo = jpy.get_type('org.esa.snap.core.datamodel.ImageInfo')
ImageLegend = jpy.get_type('org.esa.snap.core.datamodel.ImageLegend')
ImageManager = jpy.get_type('org.esa.snap.core.image.ImageManager')
JAI = jpy.get_type('')
RenderedImage = jpy.get_type('java.awt.image.RenderedImage')
GPF = jpy.get_type('org.esa.snap.core.gpf.GPF')

# Disable JAI native MediaLib extensions
System = jpy.get_type('java.lang.System')
System.setProperty('', 'true')

def write_rgb_image(bands, filename, format):
    image_info = ProductUtils.createImageInfo(bands, True, ProgressMonitor.NULL)
    im = ImageManager.getInstance().createColoredBandImage(bands, image_info, 0)
    JAI.create("filestore", im, filename, format)

file = 'D:/Union_bandas/IMAGE.SAFE/MTD_MSIL1C.xml'
product = ProductIO.readProduct(file)

image_format = 'tiff'

blue = product.getBand('B2')
green = product.getBand('B3')
red = product.getBand('B4')
infrared = product.getBand('B8')
write_rgb_image([red, green, blue, infrared], 'snappy_write_image_rgb.tiff', image_format)

Following this post Obtaining sigma (backscatter) from Sentinel 1 subset I rewrote the method to this:

def write_rgb_image(bands, filename):
    polarisation = 'B2'
    src_product = ProductIO.readProduct(filename)
    width = src_product.getSceneRasterWidth()
    height = src_product.getSceneRasterHeight()
    tar_product = Product("target", 'GeoTIFF', width, height)
    band_names = src_product.getBandNames()
    BSelectOp = BandSelectOp()
    BSelectOp.setParameter('selectedPolarisations', polarisation)
    BSelectOp.setParameter('sourceBandNames', band_names)
    BSelectOp.setParameter('bandNamePattern', '^(B2|B3|B4|B8)$')

    target_product = BSelectOp.getTargetProduct()
    ProductIO.writeProduct(target_product, 'test', 'GeoTIFF')

I’m getting a java.lang.NullPointerException and I don’t get why because I have set all the parameter of BandSelectOp