Checking for null pointer exception in an array in Jenkins scripted pipeline method

KrisT

I am injecting Active Choices parameter value(s) in the Jenkins scripted pipeline.

PFB sample values passed to active choice parameter block:

return['ABC','DEF','GHI',JKL']

PFB my sample script:

node(){
    selectModName()
}

def selectModName(){
    stage 'Multi selection'
    String[] mods = "${modName}".split(',')
    modsz = mods.size()
    echo ''+modsz+''
    for(mod in mods){
        if (modsz == null || modsz == 0){
            echo 'There is nothing to be printed'
        } else {
            echo ''+mod+' is name of the module \n'
        }
    }
}

The else block is executed when I pass greater than or equal to 1 value(s) (working as intended). But if block is not executing its logic when I don't pass any parameter and press build now.

Funny thing is- size() is returning 1 instead of 0 (echo ''+modsz+'') when values aren't passed.

How to make if block execute its logic when no values are passed?

Szymon Stepniak

Your code always jumps to the "else" block, because

"".split(',')

produces an array with a single empty string.

assert "".split(',').size() == 1
assert "".splti(',') == [""] as String[]

When you use active choice parameter with multiple values selection and you don't select anything, your variable name stores an empty string. You should check first if the modName parameter is not an empty string and only otherwise split and display values.

node(){
    selectModName()
}

def selectModName(){
    stage 'Multi selection'

    if (modName) {
        String[] mods = modName?.split(',')
        for (mod in mods) {
            echo " ${mod} is name of the module"
        }
    } else {
        echo 'There is nothing to be printed'
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Jenkins scripted pipeline or declarative pipeline

Jenkins scripted pipeline NoSuchMethodError: No such DSL method 'setLatestRevision' found among steps

Java null pointer exception after checking if null

Jenkins Scripted Pipeline: Groovy if statement

Scripted Jenkins pipeline: continue on fail

Jenkins scripted pipeline environment variable

choice equivalent in Jenkins scripted pipeline

Null pointer exception when trying to access triangular array in method

Null pointer exception in array iteration

Jenkins Scripted Pipeline use global timestamps options

Understanding Jenkins Groovy scripted pipeline code

Jenkins - export a scripted pipeline into a shared lib

Scripted Jenkins pipeline, skip parallel part of stage

Jenkins Scripted Pipeline - Is this possible to send a mail attachment

Jenkins scripted pipeline with sidecar MYSQL container for testing

How to use options in a Jenkins scripted pipeline?

Get upstream environment variables - Jenkins scripted pipeline

Jenkins scripted pipeline nested environment variable

Writing a Yaml file in Jenkins scripted pipeline

In scripted pipeline, how to get Jenkins build parameters

How to set environment variables in a Jenkins Scripted Pipeline?

Using a dockerfile with Jenkins Scripted Pipeline Syntax

Jenkins groovy scripted pipeline variables not substituting correctly

Null Pointer Exception When Checking If SharedPrefs Equals Null

Restart the pipeline from a particular stage in jenkins scripted pipeline

Null pointer exception on Object class method

Null pointer exception in setText() method of TextView

Null Pointer Exception in InputConnection.finishComposingText() method

Java HashMap get method null pointer exception

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive