title: Remove null values from Arraylist in java using normal old method
body: <p>I am trying to remove null and empty value from a Arraylist, but the following code does not work ,
  when i tried to use if(null!=a) if throwing the Nullpointer Exception , but other way works then if(a==null), Program 1 gives error but program 2 works.</p>
  
  <p>Can Someone Please explain the reason </p>
  
  <pre><code>public class RemoveNull {
  public static void main(String[] args) {
  
  
  
  
  List&lt;String&gt; arr=new ArrayList&lt;String&gt;();
  arr.add("a");
  arr.add("a");
  arr.add("a");
  arr.add("a");
  arr.add(null);
  arr.add("a");
  arr.add("a");
  arr.add(null);
  
  List&lt;String&gt; arr1=new ArrayList&lt;String&gt;();
  System.out.println(arr);
  for(String a:arr)
  
  {
      System.out.println(a);
      if(null!=a || !a.equals(""))
      {
          arr1.add(a);
          //arr.remove(a);
      }
      System.out.println(arr1);
  }
  
  System.out.println(arr);
  </code></pre>
  
  <p>}</p>
  
  <p>results</p>
  
  <p>Exception in thread "main" java.lang.NullPointerException
      at RemoveNull.main(RemoveNull.java:26)</p>
  
  <pre><code>public class RemoveNull {
  
      public static void main(String[] args) {
  
  
          ArrayList&lt;String&gt; arr=new ArrayList&lt;String&gt;();
          arr.add("a");
          arr.add("a");
          arr.add("a");
          arr.add("a");
          arr.add(null);
          arr.add("a");
          arr.add("a");
          arr.add("");
          arr.add(null);
          arr.add("");
          List&lt;String&gt; arr1=new ArrayList&lt;String&gt;();
          System.out.println(arr);
          for(String a:arr)
  
          {
              System.out.println(a);
              if(null==a || a.equals(""))
              {
  
              }
              else
              {
                  arr1.add(a);
                  //arr.remove(a);
              }
              System.out.println(arr1);
          }
          System.out.println(arr);
  
      }
  
  }
  </code></pre>
  
  <p>results </p>
  
  <p>[a, a, a, a, null, a, a, null, null]
  [a, a, a, a, a, a]</p>