posted 1 month ago
names is null, it does not point to any object.
you are trying to call a method on a null reference, which causes the exception at runtime.
Initialize the list properly
List<String> names = new ArrayList<>();
Now names refers to an actual object, so add() and the loop will work correctly.