0

I am trying with this code to replace null values in arraylist. I am getting null values in a tag in my xml file. Values in that tag are coming from arraylist. I want to remove null from tag and put nothing in place of it. My code is something like this:

    for(String s:a.getList){
here I setting values in tag by create tag and than appending child nodess using DOM parser.
}

where a=object that contains list

output is like this:

    <value>1</value>
<value>2</value>
<value>null</value>
<value>3</value>
<value>4</value>
<value>null</null>

. . .and so on

Expected output:

    <value>1</value>
<value>2</value>
<value/>
<value>3</value>
<value>4</value>
<value/>

null should be removed and tag should look something like this

code I am trying is:

  for(String s:a.list){
if(s.equals("null")){
s.replace("null","");
my code;

}

Always getting null pointer exception and don't know if this runs what will be output. Please help..

zaib7777
  • 89
  • 3
  • 14

3 Answers3

1

You are not updating the list, you are creating a new String instance since String are immutable. Just set the value you want if the current value is "null"

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

The condition won't fail for null value, but if you want to replace those, you need to add the condition because for now, it will only update "null".

AxelH
  • 14,325
  • 2
  • 25
  • 55
  • This is btw a nice use-case for a [`ListIterator`](https://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html) due to [`ListIterator#set(E)`](https://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html#set(E)). – Izruo Sep 01 '17 at 06:09
  • @Izruo it is, indeed, I don't use it enough (in my head, iterator are always read-only) ! You are welcome to post it has an answer if you like ! – AxelH Sep 01 '17 at 06:11
1

The best way to approach using array list is iterate from last to first if you want to remove concurrently.

for (int i = list.size()-1; i >= 0; i--) {
        if ("null".equalsIgnoreCase(list.get(i))) {
            list.remove(i);
        }
    }

"null".equalsIgnoreCase(list.get(i)) will avoid null pointer exception

For removing and printing value

for (String str  :  abc) {
        if ("null".equalsIgnoreCase(str)) {
            System.out.println("<value/>");
        } else {
            System.out.println("<value>"+str+"</value>");
        }
    }
  • Unfortunatly, it is still printing an XML node in the expected output. – AxelH Sep 01 '17 at 10:37
  • @AxelH could you please give me a sample of arraylist values and what is your expecting output. Hope null in your arraylist is coming as a string. I mean "null" not null. If it is not a string then you have to use null == lis.get (i) for removing condition – Arjun Nagathankandy Sep 01 '17 at 11:18
  • check OP question for that... He is printint his list in an XML formatted value. (assuming it is adding the content of the list in a node. But a `null` should output `` not simply removing it. – AxelH Sep 01 '17 at 11:22
  • @AxelH I was more concentrating on the removal on null string from arraylist concurrently. If you want something like which you have mentioned above I think second part of my answer will help you – Arjun Nagathankandy Sep 01 '17 at 11:31
  • I don't want anything... Check the names ;) – AxelH Sep 01 '17 at 11:50
0

To make it clearer

public static void main(String... args) {
    ArrayList<String> a = new ArrayList<String>();
    a.add("one");
    a.add(null);
    a.add("two");
    a.removeAll(Collections.singleton(null));
    for(String value : a) {
        System.out.println(value);
    }
}

Output

one
two
tomas
  • 830
  • 3
  • 8
  • 25
  • "_For better understanding_" ? – AxelH Sep 01 '17 at 06:13
  • hope my java is better than my english – tomas Sep 01 '17 at 06:14
  • `removeAll` is nice but it will become a problem since OP want to remove `"null"`, if those become `"NULL"`, not sure you can `removeAllIgnoreCase` (not sur there is an equivalent for lambda) – AxelH Sep 01 '17 at 06:15
  • My point is : "There is no explanation" ... you need to explain what you have done – AxelH Sep 01 '17 at 06:15
  • He said " saw this question and tried with it too..getting same exception" so i wanted to show him a working example....I think the explanation in the original question is good enough – tomas Sep 01 '17 at 06:17
  • And he said he is getting NPE....So i don't think he has "null" or "NULL" strings.... – tomas Sep 01 '17 at 06:20
  • Then you should add at least the source of the answer used, and explain that this is a working example. This, for the moment, is an incomplete answer (not really answering the question by the way, but showing an alternative) – AxelH Sep 01 '17 at 06:49