Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace Null Values in Arraylist

i'm trying to replace null values in my arrayList but I get exception

java.lang.NullPointerException

I have tried different way :

Data.replaceAll(s -> s.replaceAll(" null", "")); 

And :

for(int x = 0; x < Data.size(); x++)
          {
                if(Data.get(x).equals("null") == true)
                {
                    Data.set(x, "");
                }
          }

And :

for(int x = 0; x < Data.size(); x++)
          {
                if(Data.get(x).equals(null) == true)
                {
                    Data.set(x, "");
                }
          }

but an exception is throw java.lang.NullPointerException

Here is an exemple of my arrayList:

[0050568D6268, null, A001, A, T3, Principal, COL - Test, 4-Lock, Com. On Stage, Social, RDC, null, null, null, null, -1, null, -1, 0, -1, 99, 53]

I'm looking for any help thanks.

like image 223
PandaRasta Avatar asked Apr 14 '26 15:04

PandaRasta


1 Answers

I think you want to use map() here:

// given list data
data = data.stream()
    .map(s -> Objects.isNull(s) ? "" : s)
    .collect(Collectors.toList());

This would return a list identical to the input, except with all null values replaced by empty string.

like image 56
Tim Biegeleisen Avatar answered Apr 17 '26 02:04

Tim Biegeleisen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!