Unknown Source Java 10 [duplicate]

  • A+

This question already has an answer here:

I am getting an error while running following code:

public class TestClass {  public static void main(String[] args) {     var list = new ArrayList<String>();     list.add("Harry");     list.add("Marry");     list.add(null);     list.add("Larry");      list.removeIf(e -> e.startsWith("H"));     list.forEach(System.out::println);      } } 

Getting following error:

Exception in thread "main" java.lang.NullPointerException at test/test.TestClass.lambda$0(TestClass.java:14) at java.base/java.util.ArrayList.removeIf(Unknown Source) at java.base/java.util.ArrayList.removeIf(Unknown Source) at test/test.TestClass.main(TestClass.java:14) 

Why I am getting the unknow source error, it works fine if I provide following lambda:

list.removeIf(e -> e == null); 


null.startsWith("H") return NullPointerException instead you can have to check if the value is null or not then use startsWith:

list.removeIf(e -> e != null && e.startsWith("H")); 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: