3

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.

9
  • 1
    null and "null" are two very different things. Commented Oct 14, 2019 at 9:13
  • I tried null and "null" Commented Oct 14, 2019 at 9:13
  • Note that when asking about exceptions you should post the entire stacktrace and mark the mentioned lines in the code you've posted. Commented Oct 14, 2019 at 9:14
  • I'm using Talend so the stacktrace is like this : Exception in component tJava_3 (LOTS_ACTIF_CREMAJ) java.lang.NullPointerException at genio.lots_actif_cremaj_0_1.LOTS_ACTIF_CREMAJ.lambda$0(LOTS_ACTIF_CREMAJ.java:2158) at java.util.ArrayList.replaceAll(Unknown Source) at genio.lots_actif_cremaj_0_1.LOTS_ACTIF_CREMAJ.tJava_3Process(LOTS_ACTIF_CREMAJ.java:2158) at genio.lots_actif_cremaj_0_1.LOTS_ACTIF_CREMAJ.tDBInput_6Process(LOTS_ACTIF_CREMAJ.java:1969) Commented Oct 14, 2019 at 9:15
  • 1
    Why do you think Data.get(x).equals(null) would not throw an exception when that entry is nul? Since when can someone call a method like .equals on a null object? Commented Oct 14, 2019 at 9:16

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

You could also use .filter(Objects::nonNull) to just exclude the null elements if you don't need to replace them with some default
@JonK Yes, but my understanding of the OP is that he wants to replace and retain the originally null values.
@Tim Biegeleisen Exactly
5

The values in your list seem to be actual nulls and not strings with "null". You can replace these with "" by:

data.replaceAll(t -> Objects.isNull(t) ? "" : t);

You can remove them with:

data.removeIf(Objects::isNull)

2 Comments

I don't want to remove it I want to replace it with empty string.
@PandaRasta Added to the answer
2

in this line you are comparing the value at position x with the String null and not "with a null value":

if(Data.get(x).equals("null") == true)

Replace this comparison by:

if(Data.get(x) == null)

4 Comments

Worked perfectly! I accept the solution. An idea why Data.replaceAll(s -> s.replaceAll(null, ""));throws an exception ? Because I use it exactly like this on an other program and it worked.
it's throwing an exception because you are trying to call replaceAll on a null object when you call s.replaceAll
you should do what @rdas suggested in his answer: Data.replaceAll(s -> Objects.isNull(s) ? "" : s);
Yes but what I don't get is it's working on an other arraylist with mixing null values and string values.
2

We can not call any method on a null object, that is the reason why you get a NullPointerException.

Below line is throwing NullPointerException because calling equals method on null object is not allowed in Java.

Data.get(x).equals("null")

So replacing above with below will solve the issue.

Data.get(x) == null

Also, there is no need for the extra comparison with == true.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.